Search code examples
cassandradatastax

Cassandra Non PRIMARY KEY columns found in where clause


so I am new to Cassadra and came along with a problem, so I have a

DELETE FROM data WHERE id = 1 AND tag = 'sdasd' AND collector = 1 and end_value > 0

And end_value is not a part of any type of key, so when I execute this It throw me an error as above.

So OK I add end value as a clustering key, but later I need to update end_value and now Cassandra does not allow me to do it because it is a part of primary key. Like:

UPDATE data SET end_value=1 WHERE id=1 and tag='some tag' and collector=1

So I am stuck in a loop of some sort, I could leave it as clustering key (end value that is), but then i need to remove old record and insert new one just to update one field with seems like giant exaggeration.

I even tried to use IF instead of last condition that is

DELETE FROM data WHERE id = 1 AND tag = 'sdasd' AND collector = 1 IF end_value > 0

But here it tells me I need equality reference to uuid, but cant give those I just want to remove data with value greater then 0.

So is there any other way to do it?


Solution

  • With Cassandra, you need to be precise about the keys you specify for a delete operation. Remember, in Cassandra, a delete is really a write, so you need to be able to tell Cassandra where to put that delete. That's why you can't run a delete on non-primary key columns; because those columns are the payload, not the location where the data is stored.

    What you could do, is first query the data you want to delete. This way you can pull the key values from that data, and then run the delete with the exact keys fulfilled.