Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.1K
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda