Skip to content

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

Merged
durango merged 3 commits intosequelize:masterfrom
janmeier:multiHasMany
Sep 17, 2013
Merged

Allow include when the same table is referenced multiple times using hasMany#913
durango merged 3 commits intosequelize:masterfrom
janmeier:multiHasMany

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