Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.1K
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda