'How to find or create a record with Prisma?
Is this the best way to find or create a user in Prisma?
prisma.user.upsert({
where: {
id: user.id,
},
update: {
id: user.id,
},
create: {
id: user.id,
},
})
Solution 1:[1]
Yes, you can use upsert to create a record. If the update property is empty, the record will not be updated.
Example:
const upsertUser = await prisma.user.upsert({
where: {
email: '[email protected]',
},
update: {},
create: {
email: '[email protected]',
name: 'Test User',
},
})
We plan to document this better: how upsert can behave as a findOrCreate. The link to the GitHub issue can be found here
For more information regarding upsert, you can read more in the Prisma Client API Reference section.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nurul Sundarani |
