You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
i'm adding hasOne relationship with {as: ''} option, which leads to foreignKey in target table to be derived from alias name. Though same example is in docs, that is not what i'm expecting
hasOne method creates an association between this (the source) and the provided target.
and the description of options.as says
The alias of this model, in singular form.
as the doc says that this means the source, if there is an 'as' option then it seems normal to get aliasId rather than sourceId. I think it's just workig as same as the document implies.
@papb if there's an 'as' option when using hasOne method, the foreign key added to the target table should be sourceId ?
if it's not a bug, then I think it would be nice if the doc mentions about it more explicitly.
(I'm willing to make a pull request)
@2kindsofcs Thanks for the post, I don't think this is a bug, OP should probably use Device.belongsTo(User, { as: 'Calculator' }) instead and it would probably work as they want.
About the docs improvement, I agree they need to be improved but leave it to me, I'm already working on it so let's not duplicate work :)
@papb But that wouldn't add the mixin to the source model? (i.e you can't do user.getCalculator()?)
Edit: It is quite counter-intuitive if defining as adds a new foreign key to the target table when hasOne() is called. It might make sense for belongsTo() to do this, but not hasOne(). IMO it would be more consistent if specifying as doesn't introduce any new columns like hasMany()
For example:
User.hasMany(Subscription,{as: 'payments'});// This only affects the mixinsUser.hasOne(Developer,{as: 'dev'});// This adds devId to Developer which is quite counter-intuitiveSubscription.belongsTo(User,{as: 'payer'});// This would add payerId which to me makes sense
I understood it would be a breaking change but it would be very nice to see this consistency :)
Activity
2kindsofcs commentedon Oct 27, 2019
the same thing happens when using mysql, too.
By the way, according to the doc, it says
and the description of
options.assaysas the doc says that
thismeans the source, if there is an 'as' option then it seems normal to getaliasIdrather thansourceId. I think it's just workig as same as the document implies.@papb if there's an 'as' option when using hasOne method, the foreign key added to the target table should be
sourceId?if it's not a bug, then I think it would be nice if the doc mentions about it more explicitly.
(I'm willing to make a pull request)
papb commentedon Oct 27, 2019
@2kindsofcs Thanks for the post, I don't think this is a bug, OP should probably use
Device.belongsTo(User, { as: 'Calculator' })instead and it would probably work as they want.About the docs improvement, I agree they need to be improved but leave it to me, I'm already working on it so let's not duplicate work :)
brianle1301 commentedon Feb 28, 2021
@papb But that wouldn't add the mixin to the source model? (i.e you can't do
user.getCalculator()?)Edit: It is quite counter-intuitive if defining
asadds a new foreign key to the target table whenhasOne()is called. It might make sense forbelongsTo()to do this, but nothasOne(). IMO it would be more consistent if specifyingasdoesn't introduce any new columns likehasMany()For example:
I understood it would be a breaking change but it would be very nice to see this consistency :)