-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MongoDb: 2-way Embedded M:N Relations #2671
MongoDb: 2-way Embedded M:N Relations #2671
Conversation
8ff5a08
to
339549e
Compare
Sets to stone how these should look like, instead of hacking them together. Introduces a new many-to-many relation definition only for MongoDB: ```prisma model A { id String @id @Map("_id") @default(auto()) @test.ObjectId b_ids String[] @test.ObjectId bs B[] @relation(fields: [b_ids], references: [id]) } model B { id String @id @Map("_id") @default(auto()) @test.ObjectId a_ids String[] @test.ObjectId as A[] @relation(fields: [a_ids], references: [id]) } ``` Both models hold a list of identifiers in an array. Using this list, we build a relation to another model pointing to an identifier of that model. From the other model we again define a relation from an array to an identifier. This forms an embedded 2-way many-to-many relation with the following rules: - Detected as an embedded relation if any of the sides defines `fields` argument. - Only allowed in MongoDb. - Both sides must define `fields` and `references`. - The `fields` argument must point to an array scalar field. - The `references` argument must point to an id field that must be of same type as the `fields` array scalar. - No referential actions are allowed in these relations for now.
339549e
to
af852b7
Compare
.../src/transform/ast_to_dml/validation_pipeline/validations/relations/many_to_many/implicit.rs
Outdated
Show resolved
Hide resolved
if !relation_field.references_singular_id_field() { | ||
ctx.push_error(DatamodelError::new_validation_error( | ||
format!( | ||
"Many to many relations must always reference the id field of the related model. Change the argument `references` to use the id field of the related model `{}`. But it is referencing the following fields that are not the id: {}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Many to many relations must always reference the id field of the related model. Change the argument `references` to use the id field of the related model `{}`. But it is referencing the following fields that are not the id: {}", | |
"Implicit many to many relations must always reference the id field of the related model. Change the argument `references` to use the id field of the related model `{}`. But it is referencing the following fields that are not the id: {}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tbd but I think it's all many-to-many relations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...that was agreed on
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't know who to believe then :D #2671 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you might actually be right now. If this also applies to the new MongoDB relation, that one is not really implicit as it has the explicit arrays for storing the references.
.../src/transform/ast_to_dml/validation_pipeline/validations/relations/many_to_many/embedded.rs
Show resolved
Hide resolved
.../src/transform/ast_to_dml/validation_pipeline/validations/relations/many_to_many/embedded.rs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome
Sets to stone how these should look like, instead of hacking them together. Introduces a new many-to-many relation definition only for MongoDB:
Both models hold a list of identifiers in an array. Using this list, we build a relation to another model pointing to an identifier of that model. From the other model we again define a relation from an array to an identifier. This forms an embedded 2-way many-to-many relation with the following rules:
fields
argument.fields
andreferences
.fields
argument must point to an array scalar field.references
argument must point to an id field that must be of same type as thefields
array scalar.Closes: prisma/prisma#11553