hasMany with "as" alias not working as expected
This is a fairly straightforward bug when you use A.hasMany(B, {as:'c'}) with an as alias the foreignKey generated does not use the as alias name like it should.
This issue is caused here:
if (!this.foreignKey) {
this.foreignKey = Utils.camelizeIf(
[
Utils.underscoredIf(this.source.options.name.singular, this.source.options.underscored),
this.source.primaryKeyAttribute
].join('_'),
!this.source.options.underscored
);
}
EDIT (again): After more testing the real fix is this:
this.foreignKey = Utils.camelizeIf(
[
Utils.underscoredIf(this.isAliased ? this.options.name.singular : this.source.options.name.singular, this.source.options.underscored),
this.source.primaryKeyAttribute
].join('_'),
!this.source.options.underscored
);
This handles the case where no alias is provided, i.e. use the source table name as the foreignKey or use the alias table name if it was provided as the foreignKey.
Everything is now working as expected now with both changes.
Updated description with proper fix after more testing.
This has already been discussed, fixed, and then reverted again - see https://github.com/sequelize/sequelize/issues/6023#issuecomment-225431969. I think this comment sums it up best
The as alias is for the source model and should not be used when infering the foreign key on the target model.
Can you give a more concrete example of a case where you think the alias should be "inherited"?
I would have to respectfully disagree as it causing a confusing behavior when something like belongsTo follows said behavior when using as.
In my example I have made the following associations:
// In this case belongsTo assigns the attribute segmentId to CompanyEmployee CompanyEmployee.belongsTo(Team, {as: 'segment'}); // In this case hasMany assigned teamId to CompanyEmployee Team.hasMany(CompanyEmployee, {as: 'segment'});
Its clear in this example that the associated foreignKey should be consistent. So either make ALL of them produce teamId or all of them produce segmentId. Furthermore, I would still suggest that the foreignKey be segmentId (the aliased name) because the commonly expected foreignKey of a source table generally includes the table name in it. When using the alias it means that for all intents and purposes CompanyEmployee sees a "segment" table and has no knowledge of "team" table. And the reverse should be true as well, as a Team I am representing myself as a "segment".
There may be some legitimate case for wanting to alias a table without expecting the default foreignKey to be aliased as well but I can't think of a good one. It seems to be a completely internal effect without the right foreignKey alias as well.
Whatever the preferred result by the community is, a consistent result needs to be enforced one way or the other.
It is not typical to have the same alias on both sides. Typically your relation would look like this:
Employee.belongsTo(Team, { as: 'team' })
Team.hasMany(Employee, { as: 'members' })
It makes sense to infer the foreign key as teamId here, but it would make no sense at all to infer membersId. As @janmeier mentioned, this has been discussed thoroughly already.
@felixfbecker Ok thanks, I misunderstood what as was being was being used for in hasMany.
But then why the inconsistency with belongsTo where it applies the aliased name to the foreignKey? That still hasn't been answered.
Because it makes sense, especially if you associate the same model twice. Imagine
Team.belongsTo(User, { as: 'leader' }) // foreignKey infered as leaderId
Team.belongsTo(User, { as: 'creator' }) // foreignKey inferred as creatorId
I think I explained this very thoroughly now, if you still want to know more about the decisions please read through the linked discussions.
Thanks for the explanation.