9

I've looked around but cannot seem to find how in the world to 'insert if not exists' or 'insert on conflict' using the Exposed SQL framework for Kotlin. I am currently writing raw SQL statements and executing them as is. I don't think I can avoid that for some cases, but I would much rather use the framework if possible. I don't see any options for either DSL or DAO syntaxs.

Edit:

For anyone that may come across this, Tapac's comment worked for me.

Use Table.insertIgnore() for 'insert if not exists', and an example for upsert exists at https://github.com/JetBrains/Exposed/wiki/FAQ#q-is-upsert-supported.

Dev909
  • 115
  • 1
  • 1
  • 5
  • 4
    You can try to use `Table.insertIgnore` for 'insert if not exists' and `upsert` (https://github.com/JetBrains/Exposed/wiki/FAQ#q-is-upsert-supported) for 'insert on conflict' . – Tapac Jul 04 '18 at 09:43
  • Ah thanks! I didn't really know what .insertIgnore meant when I read through the code. Works perfectly. I haven't tried the upsert yet, but I have looked at the link, I think I should fine there now. Thanks again! – Dev909 Jul 05 '18 at 10:33
  • @Tapac would you be able to make it an answer so maybe this question could be closed as 'answered'? – mkobit Jun 08 '21 at 14:48

1 Answers1

8

You can try to use Table.insertIgnore for 'insert if not exists' and upsert github.com/JetBrains/Exposed/wiki/FAQ#q-is-upsert-supported for 'insert on conflict' .

Vlad
  • 7,997
  • 3
  • 56
  • 43
Tapac
  • 1,889
  • 1
  • 14
  • 18