I have an insert to a table, that depends on the max id of another table.
def add(languageCode: String,
typeId: Long,
properties: Seq[Property]): Unit = {
val dbAction = (
for{
nodeId <- (nodes.all returning nodes.all.map(_.id)) += Node(typeId)
language <- (languages.all filter (_.code === languageCode)).result.head
_ <- DBIO.seq(properties.map
{
property =>
val id = property.id
val name = property.key
val value = property.value
if(id == 0) {
val currentPropId: FixedSqlAction[Option[Long], h2Profile.api.NoStream, Effect.Read] = this.properties.all.map(_.id).max.result
val propertyId = (this.properties.all returning this.properties.all.map(_.id)) += Property(language.id.get, currentPropId + 1, name)
nodeProperties.all += NodeProperty(nodeId, 2, value)
} else {
nodeProperties.all += NodeProperty(nodeId, id, value)
}
}: _*)
} yield()).transactionally
db.run(dbAction)
}
As you can see, the problem is that currentPropId
is of type Rep[Option[Long]]
and of course Property
needs a proper Long
instead.
For the languageId it sufficed to add a result
to the query (languages.all filter (_.code === languageCode)).result.head
But for the currentPropId
the type then still is of FixedSqlAction
edit:
Trying it like this
if(id == 0) {
this.properties.all.map(_.id).max.map {
id =>
val propertyId = (this.properties.all returning this.properties.all.map(_.id)) += Property(language.id.get, id+1, name)
val nodeProperty = nodeProperties.all += NodeProperty(nodeId, 2, value)
propertyId andThen nodeProperty
}
Does not work, because that's not a Seq[DBIOAction]
anymore but a Seq[Object]
(Not Any
, but Object
)