Upsert queries not returning the values after conflicts.

Hi everyone, I'm trying to do query to database which would either create user and return info or select the user if it exists and return values. I came to know about doNothingconflict and it seems to not work with returning values. Could anyone please look into this?
6 Replies
Werewolf
WerewolfOP4mo ago
Query is like something like
db.insert(Table).values(values).doNothingConflict().returning()
db.insert(Table).values(values).doNothingConflict().returning()
it should return object containing the entry but it is returning empty as of now.
Darren
Darren4mo ago
onConflictDoNothing() will cancel the insert if there’s a conflict, therefore there is nothing to return as nothing has been created. The only way to return is use onConflictDoUpdate() but that has its own issues
Werewolf
WerewolfOP4mo ago
I see, what should be the workaround for this issue?
Darren
Darren4mo ago
The issue is that you're sort of creating a new row. So it can cause unwanted triggers or alter default sort order and this would now be the last item returned and you have to list all conflicting columns. It just depends on your use case if this is acceptable
Werewolf
WerewolfOP4mo ago
What would be most optimal way of handling this? Doing two calls? check whether user exists or not and deal with it accordingly?
Angelelz
Angelelz4mo ago
Just do it inside a transaction and write a separate select statement if the user already exists Or even better, do the select first and the decide based on what you get No transactions needed
Want results from more Discord servers?
Add your server