MongoDB: Many to many relation issue
See original GitHub issueBug description
see schema below
when I try to create a new record using the following code:
const doc = await this.prisma.book.create({
data: {
price,
title,
category: {
connect: { id: categoryID[0] },
},
authorID: undefined,
author: { connect: { id: authorID } },
},
});
How to reproduce
- Create a prisma schema using the provided schema and use mongo db
- Try to add categories to book and author at the same time
- See typescript error when you try to add authorId and category at the same time, when using the connect options, record inserts but get panic from prisma engine.
- Cannot query books anymore after a failed insertion
Expected behavior
It should create a collection to store the relation which is not getting created and gets an error instead
PANIC in query-engine\connectors\mongodb-query-connector\src\root_queries\write.rs:219:74
called `Option::unwrap()` on a `None` value
Prisma information
model Author {
id String @id @default(dbgenerated()) @map("_id") @db.ObjectId
firstName String
lastName String
Book Book[]
}
model Category {
id String @id @default(dbgenerated()) @map("_id") @db.ObjectId
title String
slug String @unique
books Book[]
}
model Book {
id String @id @default(dbgenerated()) @map("_id") @db.ObjectId
title String
author Author @relation(fields: [authorID], references: [id])
category Category[]
price Int
authorID String @db.ObjectId
}
Environment & setup
- OS Windows 10
- Mongodb 4.4 local install
- Nodejs 14 lts
- prisma version 2.22.1(mongodb early access)
Prisma Version
prisma : 2.22.1
@prisma/client : 2.22.1
Current platform : windows
Query Engine : query-engine 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules\@prisma\engines\query-engine-windows.exe)
Migration Engine : migration-engine-cli 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine : introspection-core 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary : prisma-fmt 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : 60cc71d884972ab4e897f0277c4b84383dddaf6c
Studio : 0.379.0
Preview Features : mongoDb
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Many to many relationship and linked table/collection
The only many-to-many solution I found is to link each connected tables to each other via DBRefs : So, for example if I...
Read more >MongoDB Many-to-Many Association - Stack Overflow
Obviously for many-to-many relationships with lots of objects you want to use a different solution (like the publisher/book example in the docs) ...
Read more >Many-to-Many Relationship in MongoDB - Model Intricate Data
To make many-to-may relationship in a relational database, we have to make a intermediary table like blog_tag that stores blog ID and tag...
Read more >MongoDB Many-to-Many Relationship with Mongoose examples
In this tutorial, I will show you how to deal with MongoDB Many to Many Relationship which is an important and complicated Relationship...
Read more >Schema Basics - Learn MongoDB The Hard Way
An N:M relationship is an example of a relationship between two entities where they both might have many relationships between each other. An...
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
@crazydev901 this is a duplicate of: https://github.com/prisma/prisma/issues/6717 where we don’t auto-fix the relations yet.
The workaround now is to manually fill in the relation. Something like:
FYI, the linked issue won’t auto-fix M:N, but rather it will auto-fix the relation to be 1:N, then you can tweak it to be M:N. This is how we do it with SQL database support right now.
Please open a new issue @BoraALAP and provide all the information the bug report template asks for. Thanks.