-
Notifications
You must be signed in to change notification settings - Fork 785
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
Explain how upsert
can behave as a hypothetical findOrCreate
#640
Comments
upsert
can behave as findOrCreate
upsert
can behave as a hypothetical findOrCreate
Would be nice to cover the usage with an empty update parameter in docs. Also having |
Example
|
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 const car = await prisma.car.findOrCreate({
where: { licensePlate: 'ABC123', country: 'NZ' }
}) |
I have the same question that @olliechick asked above |
Hey @olliechick, if license plates are scoped to country, then you can use a composite unique and then use upsert. I've created an issue for cases where that's not possible: prisma/prisma#5436 |
There is a slight hump about this. When doing an update without any update body, the I could manually add |
What @nrxus mentioned is a good point on how |
Same feeling here. Im stuck with looking for a good way to identify if the record retuner was found or created. |
I think this use case is the most compelling case for a separate |
Agreed! |
2 years later and still no solution 🥲 |
I'm with you, I just don't understand the hesitancy around adding it in. findOrCreate is similar but different than upsert :') |
Just saw this was closed and got super excited, only to find out they just added a warning to the docs 🙄. The whole point of this thread is that upsert doesn't have the flexibility of any of the |
Sorry, we haven't been able to prioritize this feature. To clarify, though, it's only the docs issue that is being closed, as the current behavior of The feature request for a dedicated |
I see, apologies for the negativity |
I don't know who this may be helpful to, but it's the closest solution I found is to use connectOrCreate :) But as it states, it's for connecting models together, but there's the findOrCreate element there, for those who want it |
Does this work atomically? For example, I have the following code:
I think if I call this function twice when there is no user with userId, then do
I think this is happening because each promise is first doing the find, then doing the create. If both happened at the same time, the exception would be impossible. |
Problem
Currently, people are wondering about having a
findOrCreate
method in the API (prisma/prisma-client-js#85).We also have recently introduced
connectOrCreate
as an experimental feature (#568).It happens that
upsert
behaves like a hypotheticalfindOrCreate
method when receiving an emptyupdate
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
upsert
documentation to cover the usage with an emptyupdate
parameter.findOrCreate
section which explains one should useupsert
as documented above to have this behaviorThe text was updated successfully, but these errors were encountered: