spacedragon spacedragon - 1 year ago 97
Scala Question

How to prevent Slick from updating the primary key column?

I'm writing a dao for updating a model user; here is the code:

case class User(id: Option[String] = None, username: String, password: String)

object Users extends Table[User]("user") {

// .... some column defines

def * = id.? ~ username ~ password <>(User, User.unapply _)

def byId(id: String) = Query(Users).filter( === id)

// will be None since id is passed as parameter.
def update(id: String, user: User)(implicit session: Session) = {

I got this error :

[error] JdbcSQLException: NULL not allowed for column "id"; SQL statement:
[error] update "user" set "id" = ?, "username" = ?, "password" = ? where "user".
"id" = 'RBq0kJAs' [23502-168] (

It seems Slick generates a SQL statement which updates the primary column "id", which has no point.
How can the id column be removed from the generated update query?

Hbf Hbf
Answer Source

According to the Slick Documentation:

Updates are performed by writing a query that selects the data to update and then replacing it with new data.

So since your query selects the id, the update statements attempts to replace it. – You may want to check out the example in the documentation (see link above).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download