我在Cassandra中有一个表,结构如下:
CREATE TABLE answers (
Id uuid,
Name text,
Description text,
LastVersion boolean,
CreationDate timestamp,
EditionDate timestamp,
PRIMARY KEY(Id, EditionDate)
)WITH CLUSTERING ORDER BY (EditionDate DESC);
问题是当我需要将LastVersion
列的值更新为false
时。在这种情况下,仅插入一个新行,其中包含Primary Key (Id, EditionDate)
的值 +LastVersion
列的值。
按此顺序:
插入:
insert into answers
(id, name, description, lastversion, creationdate, editiondate)
values
(uuid(), 'Test 1', 'Description 1', true, dateof(now()), dateof(now()));
结果:
id | editiondate | creationdate | description | lastversion | name
--------------------------------------+---------------------------------+---------------------------------+---------------+-------------+--------
ac4f9ec1-8737-427c-8a63-7bdb62c93932 | 2018-08-01 19:54:51.603000+0000 | 2018-08-01 19:54:51.603000+0000 | Description 1 | True | Test 1
更新:
update answers
set lastversion = false
where id = ac4f9ec1-8737-427c-8a63-7bdb62c93932
and editiondate = '2018-08-01 19:54:51';
结果:
id | editiondate | creationdate | description | lastversion | name
--------------------------------------+---------------------------------+---------------------------------+---------------+-------------+--------
ac4f9ec1-8737-427c-8a63-7bdb62c93932 | 2018-08-01 19:54:51.603000+0000 | 2018-08-01 19:54:51.603000+0000 | Description 1 | True | Test 1
ac4f9ec1-8737-427c-8a63-7bdb62c93932 | 2018-08-01 19:54:51.000000+0000 | null | null | False | null
怎么了?实际上,EditionTime
字段似乎有所不同,但是,我在查询上花费了相同的值UPDATE
。
更新对editionDate
使用的值与您插入的值不同,因此更新找不到原始行。 Cassandra 更新和插入实际上是更新插入,因此正在插入带有新键的新行。
请注意,EditionDate 具有毫秒精度,但您的更新仅将其指定为最接近的秒。