假设我有:

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

有帮助吗?

解决方案

在播放2如果您有自动增量长PK:

val id: Long = SQL("insert into bla bla bla").on("bleh", "blah").executeInsert().get
.

其他提示

我不使用魔法特征(因为它在播放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方法时,我特别不确定是否有并发问题。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top