Explain how `upsert` can behave as a hypothetical `findOrCreate`
See original GitHub issueProblem
Currently, people are wondering about having a findOrCreate method in the API (https://github.com/prisma/prisma-client-js/issues/85).
We also have recently introduced connectOrCreate as an experimental feature (#568).
It happens that upsert behaves like a hypothetical findOrCreate method when receiving an empty update parameter.
There has been a decision to avoid adding shortcuts in the API to already supported methods to avoid unnecessarily expand the surface to test, but having a documentation update would help people figuring that out.
Suggested solutions
- We update the
upsertdocumentation to cover the usage with an emptyupdateparameter. - Optionally (probably a bad idea): we add a
findOrCreatesection which explains one should useupsertas documented above to have this behavior
Issue Analytics
- State:
- Created 3 years ago
- Reactions:16
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Difference between findOrCreate() and upsert() in sequelize
Can someone explain the use cases for when findOrCreate() and upsert() should be used, and why upsert() has the unique constraint ...
Read more >Introduction to Upserts in Apache Pinot - Medium
Typically, upsert is a term used to describe inserting a record into a database if it does not already exist or update it...
Read more >What is upsert? How it works? - Salesforce Developers
Upsert helps avoid the creation of duplicate records and can save you time as you don't have to determine which records exist first....
Read more >What is the difference between Upsert and Indate? - IBM
If there are duplicate rows in an array, a Common Connector job with the same input data can show different behavior for "Update...
Read more >Bulk.find.upsert() — MongoDB Manual
If a matching document does exist, then the update or replacement operation ... The following describe the insert behavior of various write operations...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

What about when you want to findOrCreate based on non-unique input? (For example, finding or creating a car based on the numberplate and country.)
An upsert doesn’t work, because the
whereonly takes unique input. I would want to do something like this:There is a slight hump about this. When doing an update without any update body, the
updatedAtfield does not get updated. This is expected and probably desired BUT that means that it is not possible for us to tell if the retrieved entity is an existing entity or a created one. Looking for how to tell “updated vs new” led me to: https://github.com/prisma/prisma/discussions/3432 which explicitly suggests looking at theupdatedAtfield.I could manually add
updatedAtto the update body but that feels wrong, all I want is to find or create, and to know if it was found or created.