Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find or create a record with Prisma?

Tags:

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,
  },
})
like image 868
grabury Avatar asked Dec 06 '25 15:12

grabury


1 Answers

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.

like image 185
Nurul Sundarani Avatar answered Dec 11 '25 07:12

Nurul Sundarani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!