Search code examples
scalaplayframeworkcassandraphantom-dsl

phantom cassandra multiple tables throw exceptions


I'm using phantom to connect cassandra in play framework. Created the first class following the tutorial. Everything works fine.

case class User(id: String, page: Map[String,String])

sealed class Users extends CassandraTable[Users, User] {

  object id extends StringColumn(this) with PartitionKey[String]

  object page extends MapColumn[String,String](this)

  def fromRow(row: Row): User = {
    User(
      id(row),
      page(row)
    )
  }
}

abstract class ConcreteUsers extends Users with RootConnector {
  def getById(page: String): Future[Option[User]] = {
    select.where(_.id eqs id).one()
  }
  def create(id:String, kv:(String,String)): Future[ResultSet] = {
    insert.value(_.id, id).value(_.page, Map(kv)).consistencyLevel_=(ConsistencyLevel.QUORUM).future()
  }
}

class UserDB(val keyspace: KeySpaceDef) extends Database(keyspace) {

  object users extends ConcreteUsers with keyspace.Connector

}

object UserDB extends ResourceAuthDB(conn) {
  def createTable() {
    Await.ready(users.create.ifNotExists().future(), 3.seconds)
  }
}

However, when I try to create another table following the exact same way, play throws the exception when compile:

overriding method session in trait RootConnector of type => com.datastax.driver.core.Session;

How could I build create another table? Also can someone explain what causes the exception? Thanks.

EDIT

I moved the connection part together in one class:

class UserDB(val keyspace: KeySpaceDef) extends Database(keyspace) {
  object users extends ConcreteUsers with keyspace.Connector
  object auth extends ConcreteAuthInfo with keyspace.Connector
}

This time the error message is:

overriding object session in class AuthInfo; lazy value session in trait Connector of 
type com.datastax.driver.core.Session cannot override final member

Hope the message helps identify the problem.


Solution

  • It took me 6 hours to figure out the problem. It is because there is a column named "session" in the other table. It turns out that you need to be careful when selecting column names. "session" obviously gives the above exception. Cassandra also has a long list of reserved keywords. If you accidentally use one of them as your column name, phantom will not throw any exceptions (maybe it should?). I don't know if any other keywords are reserved in phantom. A list of them will be really helpful.