Postgresql 使用Play Framework Anorm,如何为插入获取自动生成的id?
假设我有:Postgresql 使用Play Framework Anorm,如何为插入获取自动生成的id?,postgresql,scala,playframework,anorm,Postgresql,Scala,Playframework,Anorm,假设我有: case class SomeModel( id : Pk[Long], description : String ) object SomeModel extends Magic[SomeModel] { def ayDogAy = { var aydog = SomeModel(NotAssigned, "aydog") this.insert(aydog) } } 如何取回aydog的插入id 如果重要的话,我的后台数据库是Postgres我
case class SomeModel(
id : Pk[Long],
description : String
)
object SomeModel extends Magic[SomeModel] {
def ayDogAy = {
var aydog = SomeModel(NotAssigned, "aydog")
this.insert(aydog)
}
}
如何取回aydog的插入id
如果重要的话,我的后台数据库是Postgres我没有使用魔法特性(因为它在Play 2.0中被删除),所以我不确定这是否也适用。在SQL中,可以使用SCOPE_IDENTITY()获取连接上使用的最后一个id。所以你可以做一些像
val id = SQL("SELECT SCOPE_IDENTITY()")().collect {
case Row(id: Int) => id
}.head
new SomeModel(new Id(id), "aydog")
我现在只是在玩游戏。因此,如果不进行进一步的调查,我不建议在生产中使用。当几个线程使用ayDogAy方法时,我特别不确定是否存在并发问题 在游戏2中,如果你有一个自动递增长PK:
val id: Long = SQL("insert into bla bla bla").on("bleh", "blah").executeInsert().get