How to update a field which is indexed?

195 views Asked by At

I want to update a field in Cassandra which is indexed using phantom scala sdk like:

this.update.where(_.id eqs folderId)
      .and(_.owner eqs owner)
      .modify(_.parent setTo parentId)

the parent field is a indexed field in table. But the operation is not allowed when compile the code, there will have compile exception like:

[error] C:\User\src\main\scala\com\autodesk\platform\columbus\cassandra\DataItem.scala:161: could not find implicit value for evidence parameter of type com.websudos.phantom.column.ModifiableColumn[T]

The error is caused by update the field which is indexed.

My workaround is to delete the record and insert a new record to "update" the record.

Is there a better way for the situation?

1

There are 1 answers

3
flavian On BEST ANSWER

You are not allowed to update a field that is part of the primary key, because if you do so you are rendering Cassandra unable to ever re-compose the hash of the row you are updating.

Read here for details on the topic. In essence, if you had a HashMap[K, V] what you are trying to do is update the K, but in doing so you will never be able to retrieve the same V again.

So in Cassandra, just like in the HashMap, an update to an index is done with a DELETE and then a new INSERT. That's why phantom intentionally prevents you from compiling your query, I wrote those compile time restrictions in for the specific purposes of preventing invalid CQL.