Discussion about the current state of relationships #3384
Unanswered
florianJacques
asked this question in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello everyone,
I’d like to start a discussion about the current state of relationships.
After several months working on the project, I’ve noticed issues and inconsistencies in how they are used.
Indeed, relationships like belongsTo, hasOne, etc., seem to be based on a relational (SQL-style) philosophy. Additionally, the automatic casting of BSON objects to strings before writing to the database technically works but compromises data consistency. For instance, it becomes impossible to perform a "lookup" between fields of different types (e.g., ObjectId to string).
Over the past few days, I’ve started rewriting these relationships (without breaking existing functionality). For now, I’ve called them “references.” Here are the different options:
RefTo: 1-1 relationship with the foreign key stored in the document
RefToMany: N-1 relationship with an array of foreign keys stored in the document
RefOne: 1-1 relationship with the ID stored in the linked document
RefMany: 1-N relationship with the ID stored in the linked documents
My motivations are as follows:
belongsTo, hasOne, and hasMany relationships work to some extent, but I’ve observed issues with the automatic conversion of BSON-type references.
belongsToMany relationships show the same problems and currently write to multiple collections using functions like attach, sync, etc. I find this risky, as it requires specific conditions to safely perform transactional operations with MongoDB. Moreover, pivot tables aren’t necessary in this context.
MorphTo, MorphToMany, and MorphMany relationships are redundant. MongoDB’s schemaless nature allows us to avoid empty fields in documents that may have conditional relationships.
I’d really appreciate your feedback on this matter.
Best regards.
Beta Was this translation helpful? Give feedback.
All reactions