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.

MongoDB: Many to many relation issue

See original GitHub issue

Bug 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

  1. Create a prisma schema using the provided schema and use mongo db
  2. Try to add categories to book and author at the same time
  3. 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.
  4. 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:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
matthewmuellercommented, Jun 17, 2021

@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:

model User {
  id             String   @id @default(dbgenerated()) @map("_id") @db.ObjectId
  createdAt      DateTime @default(now())
  updatedAt      DateTime @updatedAt
  ...
  favProjects    Project[]  @relation(fields: [favProjectsIds])// Worker's favorite projects
  favProjectsIds String[] @db.Array(ObjectId)
  ...
}

model Project {
  id                String     @id @default(dbgenerated()) @map("_id") @db.ObjectId
  createdAt         DateTime   @default(now())
  updatedAt         DateTime   @updatedAt
  ...
+  userIds String[] @db.Array(ObjectId)
+  users   User[]   @relation(fields: [userIds])
}

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.

0reactions
janpiocommented, Nov 8, 2021

Please open a new issue @BoraALAP and provide all the information the bug report template asks for. Thanks.

Read more comments on GitHub >

github_iconTop 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 >

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