shouldn't I not be running into unique constraint ...
# orm-help
j
shouldn't I not be running into unique constraint issues when using upsert?
r
@John Cantrell It should fail. For instance I have this schema:
Copy code
model User {
  id    Int    @id @default(autoincrement())
  email String @unique
}
And I try to add 2 users with the same email:
Copy code
await prisma.user.upsert({
    create: { email: '<mailto:user1@g.com|user1@g.com>' },
    update: { email: '<mailto:user1@g.com|user1@g.com>' },
    where: { id: 1 },
  })
  await prisma.user.upsert({
    create: { email: '<mailto:user1@g.com|user1@g.com>' },
    update: { email: '<mailto:user1@g.com|user1@g.com>' },
    where: { id: 2 },
  })
The second will fail.
💯 1
j
ah that makes sense. in my scenario though I'm really trying to achieve a 'create if not exists' using upsert.
Copy code
await prisma.user.upsert({
  where: { email: '<mailto:user1@g.com|user1@g.com>' },
  create: { email: '<mailto:user1@g.com|user1@g.com>'},
  update: {}
})
await prisma.user.upsert({
  where: { email: '<mailto:user1@g.com|user1@g.com>' },
  create: { email: '<mailto:user1@g.com|user1@g.com>'},
  update: {}
})
r
The above will not fail. This will only check if the user exists and if it does, will do nothing as your
update
block is empty.