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

Allow include when the same table is referenced multiple times using hasMany #913

merged 3 commits into from Sep 17, 2013


2 participants

janmeier commented Sep 17, 2013

This bugs happens because a hasMany assocation is saved under its combined table name in DAO.associations, even if an alias is specified. This means that only the most recent association is saved. Example:

Group.belongsTo(User, { foreignKey: 'primaryGroupId', as: 'primaryUsers' })
Group.belongsTo(User, { foreignKey: 'secondaryGroupId', as: 'secondaryUsers' })

Before, the association would be saved under Group.associations.GroupsUsers for both calls, so the second call would overwrite the primaryUsers association, making this impossible:

    include: [
        { model: User, as: 'primaryUsers' },
        { model: User, as: 'secondaryUsers' }

The call would fail telling you that the assocation primaryUsers does not exist

This PR saves the association under its alias if one is defined, in the same way that hasOne and belongsTo does. The bug only affects hasMany, but I have added corresponding tests for hasOne and belongsTo as well to make sure that no-one messes that up in the future

durango added a commit that referenced this pull request Sep 17, 2013

Merge pull request #913 from janmeier/multiHasMany
Allow include when the same table is referenced multiple times using hasMany

@durango durango merged commit 6c43b8e into sequelize:master Sep 17, 2013

1 check passed

default The Travis CI build passed

@janmeier janmeier deleted the janmeier:multiHasMany branch Sep 18, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment