为 Table 插入主键并自动递增
Inserting pirmary key for Table with auto-increment
我正在尝试找到一种方法来在主键是自动递增的 slick Table
上执行插入,但强制主键为映射大小写中的值 class . IE。给定:
case class Foo(id: Int, name: String)
class FooTable(tag: Tag) extends Table[Foo](tag, "foo") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def * = (id, name) <> (Foo.tupled, Foo.unapply)
}
val foos = TableQuery[FooTable]
如果我通过
进行常规插入
db.run(foos += Foo(123,"bar"))
它将 id
设置为下一个 自动递增 值的任何值,而不是将其设置为 123。是否有强制 [=14= 的语法] 设置为所提供案例中的值 class Foo
?
结帐forceInsert
Inserts can be a bit surprising at first […] Columns that are
auto-incremented are automatically ignored, so inserting into them has
no effect. Using forceInsert allows actual insertion into
auto-incremented columns.
我正在尝试找到一种方法来在主键是自动递增的 slick Table
上执行插入,但强制主键为映射大小写中的值 class . IE。给定:
case class Foo(id: Int, name: String)
class FooTable(tag: Tag) extends Table[Foo](tag, "foo") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def * = (id, name) <> (Foo.tupled, Foo.unapply)
}
val foos = TableQuery[FooTable]
如果我通过
进行常规插入db.run(foos += Foo(123,"bar"))
它将 id
设置为下一个 自动递增 值的任何值,而不是将其设置为 123。是否有强制 [=14= 的语法] 设置为所提供案例中的值 class Foo
?
结帐forceInsert
Inserts can be a bit surprising at first […] Columns that are auto-incremented are automatically ignored, so inserting into them has no effect. Using forceInsert allows actual insertion into auto-incremented columns.