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: '[email protected]',
name: 'Pranit1',
bank: 'VCB',
ids: [1,1]
},
{
id: 2,
email: '[email protected]',
name: 'Pranit1',
bank: 'ACB',
ids: [1,2]
},
{
id: 3,
email: '[email protected]',
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
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