Slick 3.0.0 AutoIncrement Composite Key

落爺英雄遲暮 提交于 2019-12-11 02:29:15

问题


I have a table structure as below:

Table1:
  id: Int
  name: String
  version: Int

The corresponding Slick representation of the table would be:

  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))
  }

How can I make the version to auto increment for that corresponding id?

I can have elements like:

id name version
1  n1   1
1  n2   2
2  xyz  1
3  bmp  1
3  abc  2 

So the above structure has id's 1 and 3 in versions 1 and 2, I want to have the version auto increment. If there is a feature in-built, I would like to use it. If not, I have to first issue a select, add 1 t the version and create a new record. Any suggestions?


回答1:


You will have to use triggers. That way on insert, you can ask MySQL or Postgresql to set the value of version to the result of a SQL query :

select max(version) from Table1 where id = x


来源:https://stackoverflow.com/questions/30421109/slick-3-0-0-autoincrement-composite-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!