4

I'm new to Slick 3. I want to filter composite primary key as follows that query by using Slick.

SELECT * FROM test_table WHERE (pk1, pk2) IN (("a1", "a2"), ("b1", "b2"));

I know that Slick can filter multiple conditions like

TestTableQuery.filter(t => t.pk1 === "a1" && t.pk2 === "a2")

Additionally, I know that Slick can filter multiple values(That means IN caluse.) like

val pkSeq = Seq("a1", "b1")
TestTableQuery.filter(t => t.pk1.inSet(pkSeq))

So, I want to write as describe below if it's possible.

val pk1AndPk2Seq = Seq(("a1", "a2"), ("b1", "b2"))
TestTableQuery.filter(t => (t.pk1, t.pk2).inSet(pk1AndPk2Seq))

Is there something way? Thank you.

mattsu
  • 85
  • 4
  • I think you need to define primary key in your table definition. For example `def compositePk = primaryKey("pk_a", (pk1, pk2))` and then query it like `TestTableQuery.filter(_.compositePk.inSet(pk1AndPk2Seq))` – Valerii Rusakov Jul 11 '18 at 20:38
  • Thank you for your reply. I challenge your advice, but I didn't find a "inSet" method in PrimaryKey class.. – mattsu Jul 12 '18 at 03:22
  • I am afraid that you need to use plain sql query to archive desired functionality – Valerii Rusakov Jul 12 '18 at 09:11
  • I found solution to this issue in this question https://stackoverflow.com/questions/26815913/how-to-do-or-filter-in-slick – wpik Mar 30 '20 at 10:38

0 Answers0