Error when trying to implement a One to Many ( Unique constraint failed on the constraint )
See original GitHub issueBug 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
How to reproduce
- Create two models (user, bug)
- Create a one-to-many relation between user-bug
- 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:
- Created a year ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Niwau can you share the code you are running for this.
Closing this now as it’s not actually a bug. Feel free to re-open it in case you believe we missed something.