Skip to content
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
merged 3 commits into from
Sep 17, 2013

Conversation

janmeier
Copy link
Member

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:

Group.find({
    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
Allow include when the same table is referenced multiple times using hasMany
@durango durango merged commit 6c43b8e into sequelize:master Sep 17, 2013
@janmeier janmeier deleted the multiHasMany branch September 18, 2013 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants