javascriptmysqlprisma

Unique constraint failed on the constraint: `User_Account_userId_key` in prisma


Hi I have three models

model User {
  user_id      Int      @id @default(autoincrement())
  email   String   @unique
  name    String?
  User_Account User_Account[]
}

model Account {
  account_id Int @id @default (autoincrement()) @unique
  email String 
  bank String
  createdAt DateTime @default(now())
  User_Account User_Account[]

}
model User_Account {
  id Int @id @default(autoincrement())
  accountId Int 
  userId Int 
  User User @relation(fields: [userId], references: [user_id])
  Account Account @relation(fields: [accountId], references: [account_id])
}

I am trying to seed my db like this

const data = [
    {
      id: 1,
      email: 'pranit1@mf.com',
      name: 'Pranit1',
      bank: 'VCB',
      ids: [1,1]
    },
    {
      id: 2,
      email: 'pranit1@mf.com',
      name: 'Pranit1',
      bank: 'ACB',
      ids: [1,2]
    },
    {
      id: 3,
      email: 'pranit3@mf.com',
      name: 'Pranit3',
      bank: 'VCB',
      ids: [2,3]
    }
  ]
  const users = await prisma.$transaction(
    data.map(user =>
      prisma.user.upsert({
        where: { email: user.email },
        update: {},
        create: { name: user.name,
        email:user.email },
      })
    )
  );
  
  const accounts = await prisma.$transaction(
    data.map(account => 
      prisma.account.upsert({
        where: { account_id: account.id },
        update: {},
        create: { bank: account.bank ,
          email :account.email },
      })
    )
  );

  const user_accounts = await prisma.$transaction(
    data.map(uacc =>{
      console.log(uacc);
      return prisma.user_Account.upsert({
        where: { id: uacc.id },
        update: {id: uacc.id},
        create:{
          userId: uacc.ids[0],
        accountId: uacc.ids[1] },
      })}
    )
  );

However I am getting an

Unique constraint failed on the constraint: User_Account_userId_key

The data in prisma studio is generated as shown in the image enter image description here

I am simply trying to create users and accounts and a user can be associated with multiple accounts. Their relation is shown in the User_Account table. I cant see why I am getting a unique constraint error when I dont have the @unique tag on userId


Solution

  • I was unable to reproduce the error on my side. But I suspect you already had records on the DB and they conflict with the ids of your seeder. Also, there are some improvements you could make on your schema for being simpler.

    1. Since you don't have any extra details on the many-to-many relation you can get rid of the User_Account model and let Prisma handle it for you.
    2. On the seeder, you can take advantage of the nesting features of Prisma so that you don't have to manually link the records. That way, you don't have to worry about ids.

    schema.prisma suggestion

    model User {
      id       Int       @id @default(autoincrement())
      email    String    @unique
      name     String?
      accounts Account[]
    }
    
    model Account {
      id        Int      @id @unique @default(autoincrement())
      email     String
      bank      String
      users     User[]
      createdAt DateTime @default(now())
    }
    
    

    seed.js suggestion

    const { PrismaClient } = require("@prisma/client");
    const prisma = new PrismaClient();
    
    async function main() {
        const usersData = [
            {
                email: "pranit1@mf.com",
                name: "Pranit1",
                banks: ["VCB", "ACB"],
            },
            {
                email: "pranit3@mf.com",
                name: "Pranit3",
                banks: ["VCB"],
            },
        ];
    
        const users = await prisma.$transaction(
            usersData.map((user) =>
                prisma.user.upsert({
                    where: { email: user.email },
                    update: {},
                    create: {
                        name: user.name,
                        email: user.email,
                        accounts: {
                            create: user.banks.map((bank) => ({
                                email: user.email,
                                bank,
                            })),
                        },
                    },
                })
            )
        );
    }
    
    main()
        .catch((e) => {
            console.error(e);
            process.exit(1);
        })
        .finally(async () => {
            await prisma.$disconnect();
        });