I've a table user_permissions
which has 46 permission columns along with id
and created_date
. This table has a corresponding UserPermissions
class:
class UserPermission(val id: Long,
val createdDate: Option[Timestamp],
val permission1: Boolean,
val permission2: Boolean,
...
val permission46: Boolean)
and slick mapping table
class UserPermissions(tag: Tag) extends Table[UserPermission](tag, "users_permissions") {
def * = (
id ::
createdDate ::
permission1 ::
permission2 ::
...
permission46 ::
HNil).shaped <> (
{ case x => UserPermission(
x(0), x(1), x(2), ... x(47))
},
{
UserPermission.unapply _
}
}
... <columns defined here>
)
Now I want to update UserPermission set which is identified by id
. The function that I've is:
object UserPermissions {
val userPermissions = TableQuery[UserPermissions]
def update(userPermission: UserPermission)(implicit session: Session) = {
userPermissions.filter(_.id === userPermission.id.get).update(userPermission)
}
}
This is not working and throwing Exception:
play.api.Application$$anon$1: Execution exception[[SQLServerException: Cannot update identity column 'id'.]]
which makes sense as the SQL generated by Slick is:
update "users_permissions" set "id" = ?, "created_date" = ?, ...
Problem 1
So my first problem is that I'm unable to update a full UserPermission
object with slick. If I've a solution to this problem then it would be great.
Since I'm unable to update full object then I thought to yield
the columns I want to update then fire an update query. The code looks like this:
def update(obj: UserPermission)(implicit session: Session) = {
val query = for {
p <- userPermissions
if p.id === obj.id.get
} yield (p.permission1, p.permission2, ... p.permission46)
query.update(obj.permission1, obj.permission2, ... obj.permission46)
}
Problem 2 Now slick is not updating 46 columns in query.update()
function. It can handle only 22 columns at one time. How can I update my UserPermissions
object?
One bad solution I can think is to update 22 first time, then 22 second, then 2 in third query. It'll be 3 db update queries which I don't want.
Any solutions to my problem?
Dependencies are:
scalaVersion := "2.11.4"
"com.typesafe.play" %% "play-slick" % "0.8.1"
"com.typesafe.slick" %% "slick-extensions" % "2.1.0"