Skip to content

Automatic creation of inverse side of bidirectional relationships #5144

Closed

Description

@slavafomin

Hello!

Thank you for your great work!

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:

User.hasMany(Order, {
  foreignKey: 'userId',
  onDelete: 'CASCADE',
  hooks: true
});

Order.belongsTo(User, {
  foreignKey: 'userId',
  onDelete: 'CASCADE',
  hooks: true
});

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.

Thank you!

Activity

  1. mickhansen commented on Jan 5, 2016

    @mickhansen
    Contributor

    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.

    User.Orders = User.hasMany(Order, {
      foreignKey: 'userId',
      onDelete: 'CASCADE',
      hooks: true
    });
    
    Order.belongsTo(User, {
      inverse: User.Orders,
      as: 'users'
    });
  2. verdier commented on Jan 5, 2016

    @verdier
    Contributor

    +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).

  3. slavafomin commented on Jan 5, 2016

    @slavafomin
    Author

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

    User.hasMany(Order, {
      foreignKey: 'userId',
      onDelete: 'CASCADE',
      hooks: true,
      inverse: true // Will add singular mixins on target
    });

    However, I think that two definitions are more explicit, which is good in my opinion.

  4. mickhansen commented on Jan 5, 2016

    @mickhansen
    Contributor

    @slavafomin That would work aswell in cases where no alias is defined for the relation.

  5. added
    type: feature
    on Jan 5, 2016
  6. changed the title [-]Duplicate configuration when specifying bidirectional associations?[/-] [+]Automatic creation of inverse side of bidirectional relationships[/+] on Jan 5, 2016
  7. verdier commented on Jan 5, 2016

    @verdier
    Contributor

    @mickhansen we could imagine something like that:

    User.hasMany(Order, {
      foreignKey: 'userId',
      onDelete: 'CASCADE',
      hooks: true,
      inverse: true || 'owner' // Will add singular mixins on target with {as: 'owner'}.
    });
    
  8. mickhansen commented on Jan 5, 2016

    @mickhansen
    Contributor

    @verdier That could definitely work!

    Then the user can include via [User] or {model: User, as: 'owner'} or perhaps in cases of:

    User.Orders = User.hasMany(Order, {
      foreignKey: 'userId',
      onDelete: 'CASCADE',
      hooks: true,
      inverse: 'owner' // Will add singular mixins on target
    });
    
    Order.Owner = User.Orders.inverse
    
    include: [Order.Owner]
    
  9. verdier commented on Jan 5, 2016

    @verdier
    Contributor

    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);
      delete inverseOptions.inverse;
      if(_.isString(options.inverse) inverseOptions.as = options.inverse;
      /* What about the scope ? */
      model.belongsTo(this, inverseOptions);
     }
     ...
    }
  10. verdier commented on Jan 5, 2016

    @verdier
    Contributor

    @mickhansen we could also have more options:

    User.Orders = User.hasMany(Order, {
      foreignKey: 'userId',
      onDelete: 'CASCADE',
      hooks: true,
      inverse: {
       as: 'owner',
       scope: {}
      }
    });
  11. mickhansen commented on Jan 5, 2016

    @mickhansen
    Contributor

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

  12. verdier commented on Jan 5, 2016

    @verdier
    Contributor

    @mickhansen Yep, that's true and good for inverse association reference. What about the name inverse, reverse, opposite?

  13. mickhansen commented on Jan 5, 2016

    @mickhansen
    Contributor

    Not sure what is best, inverse is 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.

  14. verdier commented on Jan 5, 2016

    @verdier
    Contributor

    Agree that inverse is for action, it's why it's not fully appropriate here.

  15. verdier commented on Jan 5, 2016

    @verdier
    Contributor

    There is also some question about what we preset in the opposite association by default, such as the hook property.

  16. mickhansen commented on Jan 5, 2016

    @mickhansen
    Contributor

    The defaults should be what's defined on the "parent" relationship i think, that would generally make sense in all cases i believe.

    hooks has no effect on belongsTo so would really only ever be specified on the hasMany side.

  17. verdier commented on Jan 5, 2016

    @verdier
    Contributor

    Is there a property which needs to be set on the opposite relation... ? Other than the potential as or options passed to inverse.

  18. mickhansen commented on Jan 5, 2016

    @mickhansen
    Contributor

    I don't believe so.

  19. overlookmotel commented on Jun 12, 2016

    @overlookmotel
    Contributor

    I also raised the same kind of issue at #6091 (should have noticed this was here first).

  20. overlookmotel commented on Jun 12, 2016

    @overlookmotel
    Contributor

    Anyone interested in the defining of associations, please also see: #6023 (comment)

  21. stale commented on Jun 29, 2017

    @stale

    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 🙂

  22. overlookmotel commented on Jun 29, 2017

    @overlookmotel
    Contributor

    Still seems like a good idea to me.

  23. self-assigned this
    on Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: feature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions