Home > Blockchain >  Slick: retrieve id of inserted value
Slick: retrieve id of inserted value

Time:09-30

Following to this part of documentation https://scala-slick.org/doc/3.0.0/queries.html#inserting, I'm trying to realise insert operation as shown above:

var point = TableQuery[Geopointtable]
val writeQuery = point returning point.map(_.id)
val geoId = writeQuery  = GeopointtableRow(0, 50, 40)

val idFromDb = db.run(geoId)

I expected, the last line of code return id of new row in table. Instead it return: "Future()". By the way, in database row creates successfully.

Is there any mistake, or I don't clear understand this part of documentation. May be there another way to retrieve id?

CodePudding user response:

This is explained in the "Getting Started" section:

Slick’s API is fully asynchronous and runs database call in a separate thread pool.

This means that db.run returns a Future that completes when the operation is finished (or fails if the operation fails). So you need to process the result inside the Future:

db.run(geoId).map{ idFromDb =>
  // Process the id here
}

The processing will happen when the database operation completes. The main thread will not block.

  • Related