Skip to content

Singularize as in foreign keys in To-Many associations (amendment of PR #5957) #6023

Closed

Description

@overlookmotel

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.

What do you think @mickhansen @janmeier @sushantdhiman?

Activity

  1. sushantdhiman commented on Jun 6, 2016

    @sushantdhiman
    Contributor

    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.

  2. mickhansen commented on Jun 6, 2016

    @mickhansen
    Contributor

    @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).

  3. overlookmotel commented on Jun 6, 2016

    @overlookmotel
    ContributorAuthor

    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.

    @sushantdhiman:

    But our transformation should not give any surprises.

    I agree. But I think getting a foreign key WorkersId is 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!

  4. mickhansen commented on Jun 6, 2016

    @mickhansen
    Contributor

    I'm leaning to your side @overlookmotel since i find it kind of horrible that foreignKey will be defacto required if you use as.

    /cc @janmeier

  5. janmeier commented on Jun 6, 2016

    @janmeier
    Member

    Thinking this over again I guess singularizing as by default is going to give us the least amount of headackes

  6. mickhansen commented on Jun 6, 2016

    @mickhansen
    Contributor

    4.0.0 is still a few weeks off (atleast), so there's time for more thought and discussion on the matter :)

  7. overlookmotel commented on Jun 6, 2016

    @overlookmotel
    ContributorAuthor

    @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...

  8. overlookmotel commented on Jun 6, 2016

    @overlookmotel
    ContributorAuthor

    Does anyone have a guess to how often inflection gets it wrong? Like 1% or 20%? In my experience probably more like 1-2%.

  9. sushantdhiman commented on Jun 6, 2016

    @sushantdhiman
    Contributor

    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

    @overlookmotel :

    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.

    @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.

  10. mickhansen commented on Jun 6, 2016

    @mickhansen
    Contributor

    Writing an additional foreignKey should not be that hard

    It's not, but we might aswell throw an error for undefined foreignKey, since as on it's own would be very poor practive.

    I think being inflected on my defined name is surprising to me. Different users will have different perspectives.

    We do know what we inflect, it would be the alias.

  11. sushantdhiman commented on Jun 6, 2016

    @sushantdhiman
    Contributor

    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

  12. felixfbecker commented on Jun 6, 2016

    @felixfbecker
    Contributor

    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.

  13. mickhansen commented on Jun 6, 2016

    @mickhansen
    Contributor

    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).

  14. overlookmotel commented on Jun 7, 2016

    @overlookmotel
    ContributorAuthor

    OK, just to rewind a bit, here's my summary of discussion so far:

    1. I think everyone agrees that singularizing as would be ideal if there was a foolproof way to do it.
    2. The concern is that inflection does not singularize with 100% accuracy.
    3. 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:

    1. Update inflection to latest before release of Sequelize v4 and pin it so it does not change until v5.
    2. 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.
    3. 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%.

  15. janmeier commented on Jun 8, 2016

    @janmeier
    Member

    +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

  16. 22 remaining items

  17. overlookmotel commented on Jun 13, 2016

    @overlookmotel
    ContributorAuthor

    @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:

    1. 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)
    2. 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.
  18. sushantdhiman commented on Jun 13, 2016

    @sushantdhiman
    Contributor
    1. as behavior 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 solution
  19. mickhansen commented on Jun 13, 2016

    @mickhansen
    Contributor

    @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

  20. overlookmotel commented on Jun 13, 2016

    @overlookmotel
    ContributorAuthor

    @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...

  21. felixfbecker commented on Jun 13, 2016

    @felixfbecker
    Contributor

    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.

  22. sushantdhiman commented on Jun 14, 2016

    @sushantdhiman
    Contributor

    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 )
    • Revert as usage for ManyToMany
    • Discuss / Implement @overlookmotel suggestions from Simplify associations #6091 (didn't get chance to have a look yet) by looking at other frameworks
  23. felixfbecker commented on Jun 14, 2016

    @felixfbecker
    Contributor

    @sushantdhiman I'm for option 2 and against 1/3.

  24. mickhansen commented on Jun 14, 2016

    @mickhansen
    Contributor

    For now, we need to revert as to foreignKey for all non-belongsTo association. We can then consider a course of action after that.

  25. overlookmotel commented on Jun 14, 2016

    @overlookmotel
    ContributorAuthor

    @mickhansen OK cool. How do we do revert? Does it need a PR? Or does git have a revert <commit sha>-type function?

  26. mickhansen commented on Jun 14, 2016

    @mickhansen
    Contributor

    You can open revert PRs from old merged PRs

  27. felixfbecker commented on Jun 14, 2016

    @felixfbecker
    Contributor

    @overlookmotel git revert <sha of the merge/squashed commit>

  28. sushantdhiman commented on Jun 14, 2016

    @sushantdhiman
    Contributor

    There is a revert button in PR, you can use it as well

  29. overlookmotel commented on Jun 15, 2016

    @overlookmotel
    ContributorAuthor

    @mickhansen Have submitted PR #6109 to revert @sushantdhiman's PR.

  30. sushantdhiman commented on Jun 20, 2016

    @sushantdhiman
    Contributor

    Merged #6109

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions