joesan
joesan

Reputation: 15435

Slick 3.0.0 AutoIncrement Composite Key

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?

Upvotes: 3

Views: 1282

Answers (1)

i.am.michiel
i.am.michiel

Reputation: 10404

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

Upvotes: 1

Related Questions