2

Is it in quill somehow possible to update multiple values on a conflict? E.g like this:

val a = quote {
  query[Product]
    .insert(_.id -> 1, _.sku -> 10)
    .onConflictUpdate((t, e) => t.sku -> (t.sku + e.sku), t.abc -> e.abc)
}

I tried it like the way above and always got a "not found: value t" error when I had two values. The documentation also does not answer questions into this direction.

Syrius
  • 941
  • 6
  • 22

1 Answers1

3

Yes, you can update multiple values on insert conflict. To do this, just provide multiple lambdas with mappings, like this

val q = quote {
  query[Product]
    .insert(lift(product))
    .onConflictUpdate(
      (t, e) => t.sku -> (t.sku + e.sku),
      (t, e) => t.abc -> e.abc)
}

This will produce the following SQL for MySQL:

INSERT INTO product (id,sku,abc) VALUES (?, ?, ?) 
ON DUPLICATE KEY UPDATE sku = (sku + VALUES(sku)), abc = VALUES(abc)
Vasiliy Ivashin
  • 425
  • 2
  • 12
  • Unfortunately this doesn't work. I get this error: "exception during macro expansion: java.lang.IllegalStateException: 'DO UPDATE' statement requires explicit conflict target" – Syrius Jan 14 '19 at 10:13
  • 1
    Then I guess you're not targeting MySQL. For other databases you have to specify the fields to test for conflicts explicitly, e.g. `.onConflictUpdate(_.id)((t, e) => ...`, that will result in code like `ON CONFLICT (id) DO UPDATE...` – Vasiliy Ivashin Jan 15 '19 at 12:26