Play Slick 2.1.0 This DBMS allows you to return only one AutoInc column from INSERT

In the following code, I can just insert my records. But I would really like to return the identifier of the inserted value, then to return the object as part of my response.

def postEntry = DBAction { request => request.body.asJson.map {json => json.validate[(String, Long, String)].map { case (name, age, lang) => { implicit val session = request.dbSession val something = Entries += Entry(None, name, age, lang) Ok("Hello!!!: " + something) } }.recoverTotal { e => BadRequest("Detected error: " + JsError.toFlatJson(e)) } }.getOrElse { BadRequest("Expecting Json data") } } 

So I tried changing the insert to:

  val something = (Entries returning Entries.map(_.id)) += Entry(None, name, age, lang) 

But I get the following exception:

 SlickException: This DBMS allows only a single AutoInc column to be returned from an INSERT 

There is a note here: http://slick.typesafe.com/doc/2.1.0/queries.html

"Note that many database systems allow you to return only one column, which should be the primary key for automatic table growth. If you request other columns, then a SlickException is thrown at run time (if the database does not support it).

But he does not say how simple it is to request an identifier column.

+5
source share
1 answer

Ende Nue above gave me a hint to find the problem. I needed the primary key and auto-increment to be marked in the table definition.

 class Entries(tag: Tag) extends Table[Entry](tag, "entries") { def id = column[Option[Long]]("id", O.PrimaryKey, O.AutoInc) def name = column[String]("name") def age = column[Long]("age") def lang = column[String]("lang") def * = (id, name, age, lang).shaped <> ((Entry.apply _)tupled, Entry.unapply _) } 

O.PrimaryKey, O.AutoInc

+7
source

Source: https://habr.com/ru/post/1201450/


All Articles