ScalaQuery multiple primary key and foreign key

How to define multiple primary key and foreign key in ScalaQuery?

object myTable1 extends Table([Int])("myTable1") { def id = column[Int]("id", O PrimaryKey) def * = id } object myTable2 extends Table([Int, Int, Int])("myTable2") { def pk1 = column[Int]("id1") def pk2 = column[Int]("id2") def fk1 = column[Int]("fk1") def * = pk1 ~ pk2 ~ fk1 } 

So, what code should I use if I want pk1 and pk2 in myTable2 to be the primary key and fk1 in myTable2 refer to id in myTable1?

+8
sql scala scalaquery
source share
1 answer

The following should work against the main ScalaQuery branch:

 object myTable2 extends Table([Int, Int, Int])("myTable2") { def pk1 = column[Int]("id1") def pk2 = column[Int]("id2") def fk1 = column[Int]("fk1") def * = pk1 ~ pk2 ~ fk1 def pk = primaryKey("pk_myTable2", pk1 ~ pk2) def fkMyTable1 = foreignKey("myTable1_fk", fk1, myTable1)(_.id) } 

Although fk1 in myTable2 is the base column, fkMyTable1 is a foreign key definition that doubles as a foreign key join. Foreign keys are available in ScalaQuery 0.9.1, explicit primary keys (with names and multi-column support) are currently available in master and will be included in 0.9.2. You can find more examples in the unit test classes ForeignKeyTest and PrimaryKeyTest.

+8
source share

All Articles