question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Explain how `upsert` can behave as a hypothetical `findOrCreate`

See original GitHub issue

Problem

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 upsert documentation to cover the usage with an empty update parameter.
  • Optionally (probably a bad idea): we add a findOrCreate section which explains one should use upsert as documented above to have this behavior

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:16
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

26reactions
olliechickcommented, Jan 29, 2021

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 where only takes unique input. I would want to do something like this:

const car = await prisma.car.findOrCreate({
  where: { licensePlate: 'ABC123', country: 'NZ' }
})
10reactions
nrxuscommented, Feb 12, 2021

There is a slight hump about this. When doing an update without any update body, the updatedAt field 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 the updatedAt field.

I could manually add updatedAt to the update body but that feels wrong, all I want is to find or create, and to know if it was found or created.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found