I am trying to seed my database and it appears prisma wants a unique foreign key where I didn't specify @unique.
Following is my schema for the two relevant tables...
model Listings {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String @db.VarChar(255)
description String?
price Float
sellerId Int
seller Users @relation(fields: [sellerId], references: [id])
}
model Users {
id Int @id @default(autoincrement())
email String @unique
username String @unique
password String
listings Listings? @relation()
}
... and my seed file.
const user1 = await prisma.users.create({
data: {
email: 'alice@gmail.com',
username: 'alice',
password: password,
},
});
await prisma.listings.create({
data:{
title: 'iPhone 11',
description: 'A new iPhone 11',
price: 100.00,
sellerId: user1.id
}
});
await prisma.listings.create({
data:{
title: 'iPhone 11',
description: 'A new iPhone 11',
price: 100.00,
sellerId: user1.id
}
});
On the second insert into Listings the following error occurs.
Environment variables loaded from .env
Running seed command `node prisma/seed.js` ...
PrismaClientKnownRequestError:
Invalid `prisma.listings.create()` invocation:
Unique constraint failed on the fields: (`sellerId`)
at Object.request (C:\Users\T\projects\souk\server\node_modules\@prisma\client\runtime\index.js:45405:15)
at async PrismaClient._request (C:\Users\T\projects\souk\server\node_modules\@prisma\client\runtime\index.js:46301:18)
at async main (file:///C:/Users/T/projects/souk/server/prisma/seed.js:68:3) {
code: 'P2002',
clientVersion: '3.14.0',
meta: { target: [ 'sellerId' ] }
}
An error occured while running the seed command:
Error: Command failed with exit code 1: node prisma/seed.js
The way I stated the relationship in the Users table definition made the relationship to the Listings table 1:1 when I wanted 1:n
...
authorUsers Transactions? @relation("Author")
recipientUser Transactions? @relation("Recipient")
listings Listings? @relation()
wallet Wallets? @relation()
}
...
TO
...
authorUsers Transactions? @relation("Author")
recipientUser Transactions? @relation("Recipient")
listings Listings[]
wallet Wallets? @relation()
}
...