默認情況下,Slick忽略O.AutoInc
標誌的列中的值,並允許數據庫在插入操作期間填充該值。如何明確指定Slick中的AutoInc列的值
但有時我需要爲自動遞增列插入一些特定值,而Slick仍然會忽略它。有沒有辦法做到這一點?
我知道,我可以做第二張桌子的定義,沒有O.AutoInc
國旗,但我正在尋找更優雅的方式來做到這一點。
更新: 這裏是我的情況類和表定義:
case class Transaction (id: Long, timestamp: LocalDateTime, comment: Option[String])
class Transactions(tag: Tag) extends Table[Transaction](tag, "tx") {
implicit val localDTtoDate = MappedColumnType.base[LocalDateTime, Timestamp] (
l => Timestamp.valueOf(l),
d => d.toLocalDateTime
)
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def timestamp = column[LocalDateTime]("ts")
def comment = column[Option[String]]("comment")
def * = (id, timestamp, comment) <> ((Transaction.apply _).tupled, Transaction.unapply)
}
如果你能做到這一點你的''-projection使用的ID柱。你能複製粘貼你的表格定義嗎? – jkinkead
@jkinkead當然! – akashihi