Hello here, I’m using this query to add a new entr...
# orm-help
b
Hello here, I’m using this query to add a new entry to my DB table. But sometimes I can have a duplicated error due to a unique constraint
(user, team)
on the
memberships
table. My aim is to use something like
INSERT IGNORE INTO ....
Is there a better way to do that?
Copy code
await prisma.membership.create({
      data: {
        permission: membership.permission.toUpperCase() as any,
        role: membership.role,
        state: MembershipState.JOINED,
        joinedAt: membership.createdAt,
        requestedAt: membership.updatedAt,
        user: {
          connect: {
            username: membership.user.username
          }
        },
        team: { connect: { slug: membership.team.slug } }
      }
Thanks in advance
Finally, I use try-catch to handle the exception like describe here. https://www.prisma.io/docs/concepts/components/prisma-client/handling-exceptions-and-errors
n
Hey Bamada 👋 upsert would be ideal for you in this case. You are essentially trying to do
findOrCreate
which can be achieved by keeping update clause as empty in upsert. This GitHub discussion will be helpful to you as it demonstrates how could you create a new record only if it doesn’t already exist.
b
I already did some tests with upsert without success😔. Because the where clause cannot be empty in my case I want to avoid to fulfill it.