Search code examples
scalacassandraphantom-dsl

Can't create Cassandra table with PartitionKey, PrimaryKey and ClusteringOrder


I'm trying to create the following Cassandra table using Phantom

  object itemId extends StringColumn(this) with PartitionKey[String]
  object anotherItemId extends StringColumn(this) with PrimaryKey[String]
  object similarity extends DoubleColumn(this) with ClusteringOrder[Double] with Descending

So I want to be able to get all records by itemId. And I want them to be ordered by similarity. I set anotherItemId as PrimaryKey because itemId, similarity composite key will not be unique. But I receive the following error:

com.websudos.phantom.exceptions.InvalidClusteringKeyException: Table similarities: When using CLUSTERING ORDER all PrimaryKey definitions must become a ClusteringKey definition and specify order.

This example shows that using PartitionKey, PrimaryKey and ClusteringOrder is possible. What am I doing wrong?


Solution

  • As the error tells you, when you specify a clustering order you need to specify one for every single part of your clustering key. If the example is suggesting otherwise, the example is wrong and I will update it now.

      object itemId extends StringColumn(this) with PartitionKey[String]
      object anotherItemId extends StringColumn(this) with ClusteringOrder[String] with Ascending
      object similarity extends DoubleColumn(this) with ClusteringOrder[Double] with Descending
    

    Remember, PRIMARY_KEY = PARTITION_KEYS + CLUSTERING_KEYS, so all clustering keys, or what phantom calls PrimaryKey, need to become ClusteringOrder when you want to define ordering.

    This is a Cassandra imposed restriction, phantom is just giving you an error faster than Cassandra would.