You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Automatic creation of inverse side of bidirectional relationships #5144
However, I'm new to Sequelize and right now it's association configuration looks somewhat counter-intuitive to me. I've used Doctrine before that for PHP. The corresponding documentation is also pretty difficult to read.
I'm trying to setup a bidirectional association between two models. Let's call them User and Order. After some time figuring out the difference between hasOne, belongsTo and hasMany associations types I've came up with the following config:
With such configuration I'm getting the correct database structure, foreign keys, accessor methods and such, however I have to duplicate options, i.e.: foreignKey, onDelete and hooks in both models. In other case I'm getting two foreign keys with different names or cascade is not set correctly.
Could you tell me if I'm doing something wrong? Or is it an actual issue with Sequelize?
In Doctrine there is a concept of owning side of the association and all options are defined only on one side of it, not on both.
It's a requirement at the time since associations don't know about eachother.
Something i'd love to change but not quite sure how since users would still like do define aliases and stuff.
Perhaps a way to define that a relation in the inverse of another.
+1 looks good ! Every time I create a relation, I ask my self about that... It's hard to predict what will take precedence (mostly for CASCADE and hooks).
changed the title [-]Duplicate configuration when specifying bidirectional associations?[/-][+]Automatic creation of inverse side of bidirectional relationships[/+]on Jan 5, 2016
I think that include should remain as before (in a first time) {model: User, as: 'owner'}. It's just sugar.
We could just create the opposite relation in hasMany (and the others):
hasMany=function(model,options)
...
if(options.inverse){inverseOptions=_.cloneDeep(options);deleteinverseOptions.inverse;if(_.isString(options.inverse)inverseOptions.as=options.inverse;/* What about the scope ? */model.belongsTo(this,inverseOptions);}...}
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
Activity
mickhansen commentedon Jan 5, 2016
It's a requirement at the time since associations don't know about eachother.
Something i'd love to change but not quite sure how since users would still like do define aliases and stuff.
Perhaps a way to define that a relation in the inverse of another.
verdier commentedon Jan 5, 2016
+1 looks good ! Every time I create a relation, I ask my self about that... It's hard to predict what will take precedence (mostly for
CASCADEandhooks).slavafomin commentedon Jan 5, 2016
@mickhansen that looks good to me too. Doctrine solves this problem in the similar fashion.
As an alternative idea, maybe we could use just a single definition instead of two:
However, I think that two definitions are more explicit, which is good in my opinion.
mickhansen commentedon Jan 5, 2016
@slavafomin That would work aswell in cases where no alias is defined for the relation.
[-]Duplicate configuration when specifying bidirectional associations?[/-][+]Automatic creation of inverse side of bidirectional relationships[/+]verdier commentedon Jan 5, 2016
@mickhansen we could imagine something like that:
mickhansen commentedon Jan 5, 2016
@verdier That could definitely work!
Then the user can include via
[User]or{model: User, as: 'owner'}or perhaps in cases of:verdier commentedon Jan 5, 2016
I think that include should remain as before (in a first time)
{model: User, as: 'owner'}. It's just sugar.We could just create the opposite relation in hasMany (and the others):
verdier commentedon Jan 5, 2016
@mickhansen we could also have more options:
mickhansen commentedon Jan 5, 2016
@verdier Well we already have support for including by association reference, so this would just be exposing the inverse relation.
Yeah inverse taking an object also makes sense :)
verdier commentedon Jan 5, 2016
@mickhansen Yep, that's true and good for inverse association reference. What about the name
inverse,reverse,opposite?mickhansen commentedon Jan 5, 2016
Not sure what is best,
inverseis something that produces the reverse of a product, so it makes sense as an "action".All three have the same meaning. Inverse might be harder for non-english proficients.
verdier commentedon Jan 5, 2016
Agree that
inverseis for action, it's why it's not fully appropriate here.verdier commentedon Jan 5, 2016
There is also some question about what we preset in the opposite association by default, such as the
hookproperty.mickhansen commentedon Jan 5, 2016
The defaults should be what's defined on the "parent" relationship i think, that would generally make sense in all cases i believe.
hookshas no effect on belongsTo so would really only ever be specified on the hasMany side.verdier commentedon Jan 5, 2016
Is there a property which needs to be set on the opposite relation... ? Other than the potential
asor options passed toinverse.mickhansen commentedon Jan 5, 2016
I don't believe so.
overlookmotel commentedon Jun 12, 2016
I also raised the same kind of issue at #6091 (should have noticed this was here first).
overlookmotel commentedon Jun 12, 2016
Anyone interested in the defining of associations, please also see: #6023 (comment)
stale commentedon Jun 29, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
overlookmotel commentedon Jun 29, 2017
Still seems like a good idea to me.