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
{{ message }}
Repository navigation
Singularize as in foreign keys in To-Many associations (amendment of PR #5957) #6023
PR #5957 recently changed the names of foreign keys in associations where an as alias is specified.
So now: Project.hasMany(User, {as: 'Workers'}) creates foreign key WorkersId rather than UserId.
This is great, but I do think the as should be singularized, so foreign key is WorkerId. This was already raised by @mickhansen in #5957, but I was too late to the party to get a comment in.
WorkersId (plural), in my view, is not what the user would expect. It's inconsistent with the naming when no as alias is provided, and with the general Sequelize convention of singularizing/pluralizing. I can imagine it tripping people up quite often because it's counter-intuitive.
Yes, using inflection to singularize as could in some few cases produce the wrong result, but in those cases the user can override it by specifying foreignKey. Personally, I think that's preferable to having to specify foreignKey in all cases in order to maintain consistent naming.
I'd be happy to make a PR for this, if there's agreement that this would be a good thing to do.
We should avoid magically transforming the name to something else, It may cause confusion once again. If user want a specific foreignKey they can always explicitly define it. But our transformation should not give any surprises.
@overlookmotel I definitely agree that WorkersId is extremely ugly - But i also remember what a huge pain in the ass inflection has been in the past, it makes it much harder for us to garantee SEMVER when inflection can break in so many subtle ways.
The problem is that it basically forces any reasonable person to ALWAYS use foreignKey when using as (because most people deploying to production should care how there database looks).
Yes I remember being caught out with inflection in the past! But inflection is still being used for pluralization within Sequelize e.g. with one-to-many relationships User.findAll({include: Order}) returns the orders as Orders (plural). So Sequelize is dependent on inflection already.
I totally agree that obeying semver is important. I'd suggest the way to guarantee that is to pin the version of inflection and only update at major version bumps of Sequelize.
But our transformation should not give any surprises.
I agree. But I think getting a foreign key WorkersIdis surprising!
I totally see that there are arguments on both sides and of course I go with the consensus. But I do feel that this could be the cause of users posting issues "why am I getting WorkersId?" which is going to be an annoyance.
In your experience, how often does inflection get it wrong? If the answer is like 1% or 5%, I'd argue it's best to cater for the majority of cases and keep it consistent with the rest of Sequelize, rather than have a behaviour which is 100% predictable but always weird!
If everyone is in agreement for singularization, it'd be good if I got on the case with a PR sooner rather than later. Personally I'd like to start building things with Sequelize v4 soon to give it a bit of a road test before release, and so it'd be preferable if breaking changes were decided on (even if not implemented yet!).
But if anyone wants to think it over further, that's another matter...
I would still prefer to keep the foreign key to match as to avoid any auto magical transformations. Being certain 100% time is better than 95% time.
Writing an additional foreignKey should not be that hard. How many associations using as are defined in a normal project may be 10 or 20, mostly in same index.js file. Defining a foreignKey (when required) should not be difficult. I think it will bring more clarity to me if I can see all the foreignKey directly in index.js
I agree. But I think getting a foreign key WorkersId is surprising!
I think being inflected on my defined name is surprising to me. Different users will have different perspectives.
Its more clear if we say, whatever you put in as will be used to form the foreignKey rather than we will try to inflect it, but we don't know what name we are going to use.
I will have no problems with whatever decision you guys take,I am sure It will be better for everyone. But if you want to use inflections just because of ugly names (which can be avoided by little developer efforts) please reconsider.
It's not, but we might aswell throw an error for undefined foreignKey, since as on it's own would be very poor practive.
Thats a good option, but should we force this upon users? If we keep using as for naming, there is no need for that. If user still want to change the foriegnKey they are free to do so.
We do know what we inflect, it would be the alias.
But can we guarantee it will be correctly converted
An id always points to one row, so it should be WorkerId, not WorkersId. Inflection is used in other places too, so this should be the default, and @mickhansen is right that any sane person uses explicit names anyway. I don't find it surprising that the alias is inflected for the column name as I only see the alias as the Model key that holds the array when including. In my (relational) database I would expect a singular, because the id points to one row.
Thats a good option, but should we force this upon users? If we keep using as for naming, there is no need for that. If user still want to change the foriegnKey they are free to do so.
Non singularized foreign key names for plural associations would be a bad standard to set, and i think we should attempt to guide users to the correct path as much as possible.
But can we guarantee it will be correctly converted
Correct, but i'm not sure this is better than the alternative :)
Most serious developers will set foreignKey for any long lived project. But for newer developers or hobby projects i think we should still attempt to enforce good practices (for 5.0 i'll probably look at introduce naming strategies and change the default one).
OK, just to rewind a bit, here's my summary of discussion so far:
I think everyone agrees that singularizing as would be ideal if there was a foolproof way to do it.
The concern is that inflection does not singularize with 100% accuracy.
Consistency needs to be maintained so that behavior doesn't change within a major release of Sequelize (i.e v4.9.1 behaves same as v4.0.0).
Now my personal view:
I think that inflection is accurate enough. It's come a long way in past couple of years. It has 600,000 downloads a month, no open issues, and the maintainer is very quick to resolve issues that do arise. They now use Wiktionary as a reference, so questions of whether behavior is correct or not can be easily resolved. I think it's safe to assume that its now working correctly for all but the most obscure words.
Personally (and you may think I'm mad) I don't want to define foreignKey for every association. This is something I want (and expect) my ORM to do for me.
To deal with the need for consistency I propose 3 things:
Update inflection to latest before release of Sequelize v4 and pin it so it does not change until v5.
Ensure that Sequelize codebase only references Utils.inflection rather than referring to inflection directly. Then, if a user finds a bug in inflection that affects their app and they want to update it to a later version, they can use Sequelize.Utils.inflection = require('inflection') to inject a later version.
The above restriction can be enforced in the test shims to throw an error if inflection is ever accessed directly.
The above would I think be the best route to do the "right" thing in 99% of cases and offer an easy, consistent way for users to deal with the 1%.
+1 on the through research on inflection @overlookmotel - Given your arguments I definitely think we should trust inflection to do the right thing.
Locking the version might be a good idea - On one hand you can argue that inflection is always right, but even an update that makes something right might break user code if the depend on the wrong behaviour. I think I agree that allowing users to inject a newer version if the need it would be a good way to go
@mickhansen OK, I see the logic. Maybe it's just a case of making the docs clearer. I'm a reasonably smart guy, but have always had trouble getting my head round it. I think if the docs made the logic that the foreign key refers to the target, that'd clear it up.
That said, if I'm the only one who finds it confusing, perhaps the only conclusion is that there's nothing unclear about it, and I'm an idiot!
A few questions remain:
Should as be used as a base for creating foreignKey in many-to-many joins? Or should it be left as was in Sequelize v3? (NB using as might add confusion as as refers to the source, and foreignKey the target)
How to fix the broken behavior of many-to-many self-associations? i.e. that the 2nd belongsToMany() statement deletes one of the keys from the through table, unless foreignKey is provided.
@sushantdhiman I'm afraid it doesn't really make sense to have them in sync, foreignkey based on alias only works when the foreign key is describing the target
@sushantdhiman As far as I can see, using as as basis of foreignKey in hasOne and hasMany makes no sense. Because the foreign key corresponds to the as of the matching belongsTo. Please see my (loooong) post above where I'm talking about hasMany and as: 'Capital'.
I think it's an open question whether as should be used for belongsToMany. I don't have a particularly strong opinion either way. But what I do know is that the implementation as it is now is broken as the as is being used for the wrong foreign key (again please see above).
I'm really sorry - I'm not having a go at you. As I've said, I find the associations API endlessly confusing, and so it's not surprising that some bugs have crept in.
Would be great if you have an opinion on how to fix it...
Finally I got a chance to read the whole post. I think everything boils down to this
Using as for foreign key base causing wrong / illogical / duplicate foreign key generation
We have to revert as behavior for ManyToMany at least.
I think this course of action would make most sense to me
Throw error when user try to use as without foreign key. This would solve the problems for inflected, illogical or duplicate foreign key at same time. ( Yes, I know we have to press a few more keys, but I think its worth it )
Activity
sushantdhiman commentedon Jun 6, 2016
We should avoid magically transforming the name to something else, It may cause confusion once again. If user want a specific
foreignKeythey can always explicitly define it. But our transformation should not give any surprises.mickhansen commentedon Jun 6, 2016
@overlookmotel I definitely agree that
WorkersIdis extremely ugly - But i also remember what a huge pain in the ass inflection has been in the past, it makes it much harder for us to garantee SEMVER when inflection can break in so many subtle ways.The problem is that it basically forces any reasonable person to ALWAYS use
foreignKeywhen usingas(because most people deploying to production should care how there database looks).overlookmotel commentedon Jun 6, 2016
Yes I remember being caught out with inflection in the past! But inflection is still being used for pluralization within Sequelize e.g. with one-to-many relationships
User.findAll({include: Order})returns the orders asOrders(plural). So Sequelize is dependent on inflection already.I totally agree that obeying semver is important. I'd suggest the way to guarantee that is to pin the version of inflection and only update at major version bumps of Sequelize.
@sushantdhiman:
I agree. But I think getting a foreign key
WorkersIdis surprising!I totally see that there are arguments on both sides and of course I go with the consensus. But I do feel that this could be the cause of users posting issues "why am I getting
WorkersId?" which is going to be an annoyance.In your experience, how often does inflection get it wrong? If the answer is like 1% or 5%, I'd argue it's best to cater for the majority of cases and keep it consistent with the rest of Sequelize, rather than have a behaviour which is 100% predictable but always weird!
mickhansen commentedon Jun 6, 2016
I'm leaning to your side @overlookmotel since i find it kind of horrible that
foreignKeywill be defacto required if you useas./cc @janmeier
janmeier commentedon Jun 6, 2016
Thinking this over again I guess singularizing
asby default is going to give us the least amount of headackesmickhansen commentedon Jun 6, 2016
4.0.0 is still a few weeks off (atleast), so there's time for more thought and discussion on the matter :)
overlookmotel commentedon Jun 6, 2016
@sushantdhiman what do you think?
If everyone is in agreement for singularization, it'd be good if I got on the case with a PR sooner rather than later. Personally I'd like to start building things with Sequelize v4 soon to give it a bit of a road test before release, and so it'd be preferable if breaking changes were decided on (even if not implemented yet!).
But if anyone wants to think it over further, that's another matter...
overlookmotel commentedon Jun 6, 2016
Does anyone have a guess to how often inflection gets it wrong? Like 1% or 20%? In my experience probably more like 1-2%.
sushantdhiman commentedon Jun 6, 2016
I would still prefer to keep the foreign key to match
asto avoid any auto magical transformations. Being certain 100% time is better than 95% time.Writing an additional
foreignKeyshould not be that hard. How many associations usingasare defined in a normal project may be 10 or 20, mostly in sameindex.jsfile. Defining aforeignKey(when required) should not be difficult. I think it will bring more clarity to me if I can see all theforeignKeydirectly inindex.js@overlookmotel :
I think being inflected on my defined name is surprising to me. Different users will have different perspectives.
Its more clear if we say, whatever you put in
aswill be used to form theforeignKeyrather than we will try to inflect it, but we don't know what name we are going to use.@mickhansen @janmeier @overlookmotel
I will have no problems with whatever decision you guys take,I am sure It will be better for everyone. But if you want to use inflections just because of ugly names (which can be avoided by little developer efforts) please reconsider.
mickhansen commentedon Jun 6, 2016
It's not, but we might aswell throw an error for undefined foreignKey, since
ason it's own would be very poor practive.We do know what we inflect, it would be the alias.
sushantdhiman commentedon Jun 6, 2016
Thats a good option, but should we force this upon users? If we keep using
asfor naming, there is no need for that. If user still want to change theforiegnKeythey are free to do so.But can we guarantee it will be correctly converted
felixfbecker commentedon Jun 6, 2016
An
idalways points to one row, so it should beWorkerId, notWorkersId. Inflection is used in other places too, so this should be the default, and @mickhansen is right that any sane person uses explicit names anyway. I don't find it surprising that the alias is inflected for the column name as I only see the alias as the Model key that holds the array when including. In my (relational) database I would expect a singular, because the id points to one row.mickhansen commentedon Jun 6, 2016
Non singularized foreign key names for plural associations would be a bad standard to set, and i think we should attempt to guide users to the correct path as much as possible.
Correct, but i'm not sure this is better than the alternative :)
Most serious developers will set
foreignKeyfor any long lived project. But for newer developers or hobby projects i think we should still attempt to enforce good practices (for 5.0 i'll probably look at introduce naming strategies and change the default one).overlookmotel commentedon Jun 7, 2016
OK, just to rewind a bit, here's my summary of discussion so far:
aswould be ideal if there was a foolproof way to do it.Now my personal view:
I think that inflection is accurate enough. It's come a long way in past couple of years. It has 600,000 downloads a month, no open issues, and the maintainer is very quick to resolve issues that do arise. They now use Wiktionary as a reference, so questions of whether behavior is correct or not can be easily resolved. I think it's safe to assume that its now working correctly for all but the most obscure words.
Personally (and you may think I'm mad) I don't want to define
foreignKeyfor every association. This is something I want (and expect) my ORM to do for me.To deal with the need for consistency I propose 3 things:
inflectionto latest before release of Sequelize v4 and pin it so it does not change until v5.Utils.inflectionrather than referring toinflectiondirectly. Then, if a user finds a bug in inflection that affects their app and they want to update it to a later version, they can useSequelize.Utils.inflection = require('inflection')to inject a later version.The above would I think be the best route to do the "right" thing in 99% of cases and offer an easy, consistent way for users to deal with the 1%.
janmeier commentedon Jun 8, 2016
+1 on the through research on inflection @overlookmotel - Given your arguments I definitely think we should trust inflection to do the right thing.
Locking the version might be a good idea - On one hand you can argue that inflection is always right, but even an update that makes something right might break user code if the depend on the wrong behaviour. I think I agree that allowing users to inject a newer version if the need it would be a good way to go
22 remaining items
overlookmotel commentedon Jun 13, 2016
@mickhansen OK, I see the logic. Maybe it's just a case of making the docs clearer. I'm a reasonably smart guy, but have always had trouble getting my head round it. I think if the docs made the logic that the foreign key refers to the target, that'd clear it up.
That said, if I'm the only one who finds it confusing, perhaps the only conclusion is that there's nothing unclear about it, and I'm an idiot!
A few questions remain:
asbe used as a base for creatingforeignKeyin many-to-many joins? Or should it be left as was in Sequelize v3? (NB usingasmight add confusion asasrefers to the source, andforeignKeythe target)belongsToMany()statement deletes one of the keys from the through table, unlessforeignKeyis provided.sushantdhiman commentedon Jun 13, 2016
asbehavior should be in sync, otherwise we will have to reopen Behaviour of alias (as) to foreignKey translations in associations should be synchronized. #5267 and/or find better solutionmickhansen commentedon Jun 13, 2016
@sushantdhiman I'm afraid it doesn't really make sense to have them in sync, foreignkey based on alias only works when the foreign key is describing the target
overlookmotel commentedon Jun 13, 2016
@sushantdhiman As far as I can see, using
asas basis offoreignKeyinhasOneandhasManymakes no sense. Because the foreign key corresponds to theasof the matchingbelongsTo. Please see my (loooong) post above where I'm talking abouthasManyandas: 'Capital'.I think it's an open question whether
asshould be used forbelongsToMany. I don't have a particularly strong opinion either way. But what I do know is that the implementation as it is now is broken as theasis being used for the wrong foreign key (again please see above).I'm really sorry - I'm not having a go at you. As I've said, I find the associations API endlessly confusing, and so it's not surprising that some bugs have crept in.
Would be great if you have an opinion on how to fix it...
felixfbecker commentedon Jun 13, 2016
Finally had the time to read through @overlookmotel's post, he raises valid points and I agree with point 1 and 2 of his conclusions.
sushantdhiman commentedon Jun 14, 2016
Finally I got a chance to read the whole post. I think everything boils down to this
asfor foreign key base causing wrong / illogical / duplicate foreign key generationasbehavior forManyToManyat least.I think this course of action would make most sense to me
aswithout foreign key. This would solve the problems for inflected, illogical or duplicate foreign key at same time. ( Yes, I know we have to press a few more keys, but I think its worth it )asusage forManyToManyfelixfbecker commentedon Jun 14, 2016
@sushantdhiman I'm for option 2 and against 1/3.
mickhansen commentedon Jun 14, 2016
For now, we need to revert
astoforeignKeyfor all non-belongsTo association. We can then consider a course of action after that.overlookmotel commentedon Jun 14, 2016
@mickhansen OK cool. How do we do revert? Does it need a PR? Or does git have a
revert <commit sha>-type function?mickhansen commentedon Jun 14, 2016
You can open revert PRs from old merged PRs
felixfbecker commentedon Jun 14, 2016
@overlookmotel
git revert <sha of the merge/squashed commit>sushantdhiman commentedon Jun 14, 2016
There is a revert button in PR, you can use it as well
overlookmotel commentedon Jun 15, 2016
@mickhansen Have submitted PR #6109 to revert @sushantdhiman's PR.
sushantdhiman commentedon Jun 20, 2016
Merged #6109