spacedragon spacedragon - 17 days ago 5
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 === id)

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


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] (DbException.java:169)


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

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).

Comments