MongoDB: Cannot create m-n relations
See original GitHub issueBug description
I cannot create Many to Many relations (MongoDB). I followed the documentation, but this leads to the following error:
Prisma schema loaded from prisma/schema.prisma
Error: Schema parsing
error: Error parsing attribute "@relation": The type of the field `categoryIDs` in the model `Post` is not matching the type of the referenced field `id` in model `Category`.
--> schema.prisma:14
|
13 | categoryIDs String[] @db.Array(ObjectId)
14 | categories Category[] @relation(fields: [categoryIDs])
15 | }
|
error: Error parsing attribute "@relation": The type of the field `postIDs` in the model `Category` is not matching the type of the referenced field `id` in model `Post`.
--> schema.prisma:20
|
19 | postIDs String[] @db.Array(ObjectId)
20 | posts Post[] @relation(fields: [postIDs])
21 | }
|
Validation Error Count: 2
How to reproduce
- Create a prisma schema with the example of the documentation
- npx prisma generate
Expected behavior
No response
Prisma information
datasource db {
provider = "mongodb"
url = "********"
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["mongoDb"]
}
model Post {
id String @id @default(dbgenerated()) @map("_id") @db.ObjectId
categoryIDs String[] @db.Array(ObjectId)
categories Category[] @relation(fields: [categoryIDs])
}
model Category {
id String @id @default(dbgenerated()) @map("_id") @db.ObjectId
postIDs String[] @db.Array(ObjectId)
posts Post[] @relation(fields: [postIDs])
}
Environment & setup
- OS: Windows with WSL2 (Ubuntu 20.04)
- Database: MongoDB
- Node.js version: 14.18.1
Prisma Version
prisma : 3.4.0
@prisma/client : 3.4.0
Current platform : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine : migration-engine-cli 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine : introspection-core 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary : prisma-fmt 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash : 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85
Studio : 0.438.0
Preview Features : mongoDb
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Data modeling question on an M:N relationship - MongoDB
I learned in the amazing Mongo University Data Modeling course that typically a many-to-many relationship is modeled as two collections, ...
Read more >MongoDB Relationships: A Quick Guide - KnowledgeHut
We can create one-to-many relationships between data using embedded documents, allowing us to retrieve data quickly with few read operations.
Read more >Many-to-many relations - Prisma
Many-to-many (m-n) relations refer to relations where zero or more records on one side of the relation can be connected to zero or...
Read more >MongoDB - Create a Relationship - Quackit Tutorials
To create a relationship in MongoDB, either embed a BSON document within another, or reference it from another. MongoDB databases work differently to ......
Read more >Dmitri Pisarev on Twitter: "MAYDAY, MAYDAY, @prisma 3.4 came ...
MongoDB: Cannot create m-n relations · Issue #10071 · prisma/prisma. Bug description I cannot create Many to Many relations (MongoDB).
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 Free
Top 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
We just released a patch, you can now use the
3.4.1
version that includes the fix.We identified the issue and are working on a fix. That work will be tracked there: https://github.com/prisma/prisma/issues/10105