Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple insertOrUpdate (insert ... on duplicate key update) #8134

Closed
dotMortis opened this issue Jul 7, 2021 · 9 comments
Closed

Simple insertOrUpdate (insert ... on duplicate key update) #8134

dotMortis opened this issue Jul 7, 2021 · 9 comments

Comments

@dotMortis
Copy link

Problem

I want to create an ez "insert ... on duplicate key update" query.
I see no reason why this should only be possible via a raw query.

prisma.myTable.insertOrUpdate({data: {a: 1, b: 2, c: 3}, overwrite: {a: true, c: true}})
@pantharshit00
Copy link
Contributor

Doesn't upsert solve this for you: https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#upsert

Upsert basically will insert if where condition is not met, otherwise it will update the record which sounds like what you are describing.

@dotMortis
Copy link
Author

@pantharshit00 Thank you for your answer.
Sure, it works, as far as I can see.
But why not use the native function from the database? :D
Would be nice to see this in a future release.

@janpio
Copy link
Member

janpio commented Jul 15, 2021

Isn't upsert, that @pantharshit00 linked to, doing exactly that? What is the difference you are hoping to see here?

@windowsdeveloperwannabe
Copy link

Isn't upsert, that @pantharshit00 linked to, doing exactly that? What is the difference you are hoping to see here?

The current upsert offered by Prisma has a race condition (#3242). It would be optimal if at least the Postgres/Mysql/Mongodb drivers used the database's native upsert to avoid that.

I currently have to use raw SQL to do upserts to continue using Prisma. But other than that it's such a nice library 🥰

@shtse8
Copy link

shtse8 commented Aug 7, 2022

Doesn't upsert solve this for you: https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#upsert

Upsert basically will insert if where condition is not met, otherwise it will update the record which sounds like what you are describing.

Does upsert cannot work in $transaction? as it returns Prisma.Prisma__DataClient<Data> instead of PrismaPromise.
I tried to use upsert in transaction, but the executed upsert before error didn't roll back.


update:
it doesn't work in $transaction([]), but it works well in interactive transaction $transaction(prisma => {}).

@Akxe
Copy link
Contributor

Akxe commented Oct 17, 2022

The upsert solves this for a single row, but upsert many are still missing and thus, doing something like refreshing imported data is very complex of must be done manually.

INSERT ... ON DUPLICATE KEY UPDATE would solve this.
(related: #10362)

@taylor-lindores-reeves
Copy link

@Akxe suppose I have an API that allows car dealerships to post their stock feed to my website. I want my API to be in sync with their website, so when they mark a car as sold, it is also marked as sold on my website. Any idea how to achieve this with Prisma in its current form?

Thanks in advance!

@janpio
Copy link
Member

janpio commented Nov 1, 2022

I am closing this for now, please open a new issue if you need MySQL's INSERT ... ON DUPLICATE KEY UPDATE specifically (support for PostgreSQL's ON CONFLICT will be added in 4.6.0, an update will be posted in #3242)

@janpio janpio closed this as not planned Won't fix, can't repro, duplicate, stale Nov 1, 2022
@taylor-lindores-reeves
Copy link

taylor-lindores-reeves commented Nov 1, 2022

Actually I managed to achieve what I was looking for using create/update in nested writes within a for... of loop.

I will take a look into PostGreSQL's ON CONFLICT. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants