6

I have the following Cassandra table:

create table start_stop (id text, start text, end text, price double, PRIMARY KEY (id, start));

I do an insert like this:

insert into start_stop (id, start) values ('123', 'w');

Now I want to do an update:

update start_stop set end = 'z' where id = '123';

and I get an error: InvalidRequest: code=2200 [Invalid query] message="Some clustering keys are missing: start"

How can I fix this, aside from doing a query to look up the start value before doing an update?

jcm
  • 5,499
  • 11
  • 49
  • 78

2 Answers2

8

Your primary key is incomplete, and cassandra can only update rows if there is a primary key match.

If you look at your table structure, (id, start) combined makes primary key, out of which id is partition key.

In cassandra there is no difference between update and insert, but in either case, you need to have complete primary key, for cassandra to find specific row.

You need to use either of below to update.

update start_stop set end = 'z' where id = '123' and start='w';

insert into start_stop (id, start, end) values ('123', 'w', 'z');
Abhishek Anand
  • 1,940
  • 14
  • 27
1

Easy, you must provide clustering column values too. (your PK is PRIMARY KEY (id, start), that means id is Partition Key and start is clustering column.

Both Partition Key and Clustering Column are the part of Primary Key and must unique. So, if you update with only id in WHERE clause, it will ambiguous.

e.g. PRIMARY KEY (year, month), if you update with UPDATE table SET value="x" WHERE year=2016, cassandra will not know which row you are really want to do update.

So, you need to provide the complete PRIMARY KEY in your UPDATE. :)

madooc
  • 89
  • 5