question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when trying to implement a One to Many ( Unique constraint failed on the constraint )

See original GitHub issue

Bug description

I am trying to make a one to many relation, where a user can report many bugs. But when I try to make a user post bugs this error occurs: Unique constraint failed on the constraint: Bug_authorId_key

image

How to reproduce

  1. Create two models (user, bug)
  2. Create a one-to-many relation between user-bug
  3. Try make a user create more than one bug

Expected behavior

The user creates many posts.

Prisma information

model User {
  id Int @id @default(autoincrement())
  name String 
  email String @unique
  password String
  role Role @default(User)
  bugs Bug[] 
}

model Bug {
  id Int @id @default(autoincrement())
  title String
  description String
  status Status
  author User @relation(fields: [authorId], references: [id])
  authorId Int 
}

enum Role {
  User
  Admin
}

enum Status {
  Solved
  High
  Medium
  Low
}

Environment & setup

  • OS: Windows
  • Database: MySQL
  • Node.js version: v16.16.0

Prisma Version

prisma                  : 4.5.0
@prisma/client          : 4.5.0
Current platform        : windows
Query Engine (Node-API) : libquery-engine 0362da9eebca54d94c8ef5edd3b2e90af99ba452 (at node_modules\@prisma\engines\query_engine-windows.dll.node)
Migration Engine        : migration-engine-cli 0362da9eebca54d94c8ef5edd3b2e90af99ba452 (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine    : introspection-core 0362da9eebca54d94c8ef5edd3b2e90af99ba452 (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary           : prisma-fmt 0362da9eebca54d94c8ef5edd3b2e90af99ba452 (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Format Wasm             : @prisma/prisma-fmt-wasm 4.5.0-43.0362da9eebca54d94c8ef5edd3b2e90af99ba452
Default Engines Hash    : 0362da9eebca54d94c8ef5edd3b2e90af99ba452
Studio                  : 0.476.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
garrensmithcommented, Oct 25, 2022

@Niwau can you share the code you are running for this.

0reactions
jkomynocommented, Oct 27, 2022

Closing this now as it’s not actually a bug. Feel free to re-open it in case you believe we missed something.

Read more comments on GitHub >

github_iconTop Results From Across the Web

One-To-Many relation giving unique constraint error #13225
So I came across a really weird behaviour from my schema and I'm not quite sure if I'm doing this right. What happens...
Read more >
SQLite3 UNIQUE constraint failed error - Stack Overflow
You get a UNIQUE constraint failed error when the data that you are inserting has an entry which is already in the corresponding...
Read more >
SQLite UNIQUE Constraint
In this tutorial, you will learn how to use the SQLite UNIQUE constraint to ensure all values in a column or a group...
Read more >
How To Handle Prisma Unique Constraints with a Friendly Error
Prisma defines many error codes and one of them P2002 is for an error when the Unique constraint failed . Therefore, we can...
Read more >
SQLite Constraint - w3resource
A COMPOSITE FOREIGN KEY constraint is one where the child and parent keys are both composite keys, that is, two or more columns...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found