0

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)

1 Answers1

0

You cannot mix the lifted types (Rep[_]) with standard Scala types. You can map over your SqlAction to extract the needed type.

val currentPropId: FixedSqlAction[Option[Long], NoStream, Effect.Read] = ???
currentPropId.flatMap { id: Option[Long] =>
  ???
}

In your exact case, something like this:

if(id == 0) {
  properties.map(_.id).max.result.flatMap { id: Option[Long] =>
    val propertyId = (properties returning properties.map(_.id)) += Property(language.id.get, id.map(_ + 1), name)
    val nodeProperty = nodeProperties += NodeProperty(nodeId, 2, value)

    propertyId andThen nodeProperty
  }
}
...
Radu Gancea
  • 773
  • 10
  • 19
  • and for my concrete example? (wrapping everything in the map doesn't seem to work) –  Nov 12 '17 at 20:51
  • not everything, just that part. why it does not work? the idea is to "chain" the actions – Radu Gancea Nov 12 '17 at 21:08
  • Well, I would need to wrap everything after that `currentPropId` into the `map` because how else can I use the value in the next `statements`? (see my edit above, please) –  Nov 12 '17 at 21:25
  • Fixed. Try `currentPropId.flatMap {...}` – Radu Gancea Nov 12 '17 at 21:40
  • I missed the .result before the map (now it compiles with map and flatmap as the structure already is flat!). I will accept the answer now, although I face the same problem with the `this.properties.all returning...` line and there a `map` or even `result` is not working –  Nov 13 '17 at 21:35