Kotlin Exposed - How to insert if not exists, and on conflict?
Asked Answered
S

1

12

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.

Swats answered 3/7, 2018 at 15:35 Comment(3)
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' .Brickyard
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!Swats
@Brickyard would you be able to make it an answer so maybe this question could be closed as 'answered'?Exhale
B
10

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' .

Brickyard answered 9/6, 2021 at 15:56 Comment(1)
why is upsert available when using DSL but not when using DAOCaines

© 2022 - 2024 — McMap. All rights reserved.