Slick 3.0.0 AutoIncrement 组合键
Slick 3.0.0 AutoIncrement Composite Key
我有一个 table 结构如下:
Table1:
id: Int
name: String
version: Int
table 对应的 Slick 表示为:
class Table1(tag: Tag) extends Table[(Int, String, Int)](tag, "TABLE1") {
def id = column[Int]("ID")
def name = column[String]("NAME")
def version = column[Int]("VERSION")
def pk = primaryKey("pk_a", (id, version))
}
如何让版本自动递增对应的id?
我可以有这样的元素:
id name version
1 n1 1
1 n2 2
2 xyz 1
3 bmp 1
3 abc 2
所以上面的结构在版本 1 和 2 中有 id 的 1 和 3,我想让版本自动递增。如果有内置功能,我想使用它。如果不行,我只好先发一个select,加1t版本,新建一条记录。有什么建议吗?
您将不得不使用触发器。这样在插入时,您可以要求 MySQL 或 Postgresql 将 version
的值设置为 SQL 查询的结果:
select max(version) from Table1 where id = x
我有一个 table 结构如下:
Table1:
id: Int
name: String
version: Int
table 对应的 Slick 表示为:
class Table1(tag: Tag) extends Table[(Int, String, Int)](tag, "TABLE1") {
def id = column[Int]("ID")
def name = column[String]("NAME")
def version = column[Int]("VERSION")
def pk = primaryKey("pk_a", (id, version))
}
如何让版本自动递增对应的id?
我可以有这样的元素:
id name version
1 n1 1
1 n2 2
2 xyz 1
3 bmp 1
3 abc 2
所以上面的结构在版本 1 和 2 中有 id 的 1 和 3,我想让版本自动递增。如果有内置功能,我想使用它。如果不行,我只好先发一个select,加1t版本,新建一条记录。有什么建议吗?
您将不得不使用触发器。这样在插入时,您可以要求 MySQL 或 Postgresql 将 version
的值设置为 SQL 查询的结果:
select max(version) from Table1 where id = x