Thank you cvogt for helping out in this discussion.
I think it would be helpful to submit a PR, inasmuch as it is a very common and useful functionality which should not be missing in slick's plainSQL queries.
Finally, i found a work-around to replace the missing native function as following.
Within the same session I settle two queries. The first one is the INSERT
statement, the second statement is SELECT LAST_INSERT_ID()
which returns the newest automatically generated value that was set for the AUTO_INCREMENT
column by the recently executed INSERT
(1). More details here: MySQL Reference - LAST_INSERT_ID()
Database.forDataSource(dataSource).withDynSession {
sqlu"""INSERT INTO `users`(`email`) VALUES ("theEmailAdress@test.de")
""".firstOption match {
case Some(num) if num == 1 => sql"SELECT LAST_INSERT_ID()".as[Long].firstOption()
case None => None
}
}
This works for me right now. If there are any improvements, do not hesitate to post your solution.