Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.1K
Views
Prisma Unique constraint failed on the fields: (`sellerId`)

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
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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()
}
...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!