Scala anorm restore inserted id

I have a table with autoid for the id field. After inserting the line with the anom, I would like to get the generated identifier. Any idea?

SQL( """ insert into accom (id,places,lat,lon,permaname,country,defaultLanguage) values ( {places}, {lat}, {lon}, {permaname}, {country}, {defaultLanguage} ) """).on( 'id -> id, 'places -> places, 'lat -> lat, 'lon -> lon, 'permaname -> permaname, 'country -> country, 'defaultLanguage -> defaultLanguage).executeUpdate() } 
+4
source share
3 answers

In the latest version, you need a scalar:

  val newId = SQL( """ insert into accom (places,lat,lon,permaname,country,defaultLanguage) values ({places}, {lat}, {lon}, {permaname}, {country}, {defaultLanguage}) """).on( 'places -> places, 'lat -> lat, 'lon -> lon, 'permaname -> permaname, 'country -> country, 'defaultLanguage -> defaultLanguage).executeInsert(scalar[Long].single) 
+7
source

Use executeInsert instead of executeUpdate , and the return value is id.

+2
source

Use the executeInsert method instead of executeUpdate ; it returns Option[T] , where T is the type of primary key.

Perhaps you should take a look at How to get the primary key when saving a new object to Anorm , which also shows how to formulate the INSERT without specifying the identifier — which you want to generate.

+2
source

All Articles