Skip to content

Several bugs/questions regarding underscored/underscoredAll in Sequelize v5 #11225

Closed

Description

@papb

Several issues related to the use of underscore (and the now inexistent underscored_all) have been opened recently:

#10565
#10754
#10857
#10971
#10977
#11037
#11047
#11062
#11112
#11149
#11154
#11162
#11306
#11417
#11423
#11574

I am creating this issue to track them all in a single place.

I haven't looked deeply into it yet, but I believe some of them are caused mostly by a misunderstanding of how to upgrade from v4 to v5 (most likely the first thing I will do is expand on the documentation on how to do it correctly, I agree that the docs didn't go too deep into this).

I'm convinced this is not just a docs issue; this is something that should change / be improved.

Stay tuned!

Activity

  1. added
    type: bug
    docs
    on Jul 25, 2019
  2. self-assigned this
    on Jul 25, 2019
  3. frastlin commented on Jul 29, 2019

    @frastlin

    Well, the documentation is rather clear on how to use the underscored functionality, and if there is another way to use it, I would love to know.
    I'm making a couple hooks today for my server to simulate the underscored functionality until this gets resolved.
    I don't mind how the tables look in the database, when I use this, I want foreignId to be foreign_id when I interact with items in the database.

  4. mb8z commented on Aug 19, 2019

    @mb8z

    @frastlin Would you like to share those hooks? I would greatly appreciate it! 🎉

  5. frastlin commented on Aug 19, 2019

    @frastlin

    My solution is pretty unique to FeathersJS, the framework I'm using.
    I have a before hook and an after hook. The before hook checks on a create method, if any of the keys in the objects end with "_id". If they do, they are added to a list. Then that list is checked if the key.slice(0, key.length-3) is in the list of hooks that I have manually created. If it is, then the hook is run through a camelize function.
    The after hook also has a list of keys that may have the Id after them. So on find and get functions, I filter all the keys by the ones that end with "Id", then check if the key without "Id" is in the list of keys that I provided. Then I run an underscore function on the resulting keys.
    It's not the best method and I really can't wait until this bug is fixed!

  6. mb8z commented on Aug 19, 2019

    @mb8z

    @frastlin Thanks a lot for the input. The bug is indeed frustrating. Cost me spending at least 2 hours of looking what I do wrong that it does not work.

  7. matiasmenker commented on Aug 19, 2019

    @matiasmenker

    Some update about the issues? Some one that solve this problem?

  8. matiasmenker commented on Aug 19, 2019

    @matiasmenker

    Well, the documentation is rather clear on how to use the underscored functionality, and if there is another way to use it, I would love to know.
    I'm making a couple hooks today for my server to simulate the underscored functionality until this gets resolved.
    I don't mind how the tables look in the database, when I use this, I want foreignId to be foreign_id when I interact with items in the database.

    Can you please share?

  9. papb commented on Aug 19, 2019

    @papb
    MemberAuthor

    Hello, just passing by to say that I did not forget about this. Finding time is not easy... Thanks everyone for the patience so far 👍

  10. yoshiya0503 commented on Sep 3, 2019

    @yoshiya0503

    [My workaround plan until fixed]

    [1] first of all, set global configuration as follows.

    // your connection.js
    const options = {
      define: {
        underscored: true,
        timestamps: false
      }
    };
    module.exports = new Sequelize(database, username, password, options);
    
    

    [2] define "foreign key" and "created_at, updated_at" to your model explicitly like as follows.

    module.exports = class Project extends Sequelize.Model {
      static init() {
        return super.init({
          name: { type: Sequelize.STRING, allowNull: false },
          created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.NOW },
          updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.NOW },
        }, {
          sequelize,
          modelName: 'project',
        });
      }
    
      static associate(models) {
        Project.hasMany(models.service, { foreignKey: 'project_id' });
       }
    };
    
    module.exports = class Task extends Sequelize.Model {
      static init() {
        return super.init({
          project_id: { type: Sequelize.INTEGER, allowNull: false },
          status: { type: Sequelize.STRING, allowNull: true },
          created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.NOW },
          updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.NOW },
        }, {
          sequelize,
          modelName: 'task',
        });
      }
    
      static associate(models) {
        Task.belongsTo(models.project, { foreignKey: 'project_id' });
      }
    };
    
  11. serge1peshcoff commented on Sep 3, 2019

    @serge1peshcoff

    For those stumbled upon this issue, here's how I've fixed it:

    1. converted timestamps to lowercase manually, like this:
    const Model = sequelize.define('application', {
        // some fields
    }, {
        underscored: true,
        createdAt: 'created_at', // <====== this line and the following one
        updatedAt: 'updated_at',
    }
    1. converted all the foreign keys to lowercase like this:
    // Declaring 2 models, Parent and Child, Child model has the reference to Parent model
    // via 'parent_id' foreign key.
    
    Parent.hasMany(Child, { foreignKey: 'parent_id' }); // note the 'foreign_key' parameter
    Child.belongsTo(Parent, { foreignKey: 'parent_id' }); // and here as well
  12. added
    P1: important
    on Sep 14, 2019
  13. 16 remaining items

  14. added a commit that references this issue on May 29, 2020
  15. joshparolin commented on Aug 11, 2020

    @joshparolin

    Just a note that you still have to assign a "foreignKey" value and explicitly name your underscored value as of v6.3.3. Otherwise, findAll will return both your underscored version and a camel case version.

  16. sfratini commented on Sep 29, 2020

    @sfratini

    In addition to what @joshparolin is saying, that is still not working on some cases. I have all my foreign keys specified as snake_case and when i try to run a setXXX with a many-to-many relationship it fails but only when I am trying to add new records to the join table. Not if I try to delete.

    Eg. if the community has features [1, 2, 3] and I run setFeatures([1,2]) it works. But if the community has [1, 2, 3] and I run setFeatures([3, 4, 5]) it breaks.

    But the error that I get is this:

    
    {
                        "message": "CommunityFeature.idDdFeature cannot be null",
                        "type": "notNull Violation",
                        "path": "idDdFeature",
                        "value": null,
                        "origin": "CORE",
                        "instance": {
                          "idCommunity": null,
                          "idDdFeature": null,
                          "id_community": 1,
                          "id_dd_feature": 2
                        },
                        "validatorKey": "is_null",
                        "validatorName": null,
                        "validatorArgs": []
                      }
                    ]
    

    As you can see, the instance has both camelCase and snake_case but in this case only one of them has the right values so the entire query is failing.

    My goal is to use snake_case on the DB but camelCase on my GraphQL API.

    My solution was to actually point my join table model foreignKeys to the camelCase that I defined on the model. While I can see why this works, these are some manual steps I don't think I should be doing when all the information for the fields is available:

    Community.belongsToMany(DdFeature, {
            as: 'features',
            through: CommunityFeature,
            foreignKey: 'idCommunity', //This is the field I define in the model
            otherKey: 'idDdFeature', ///This is the field I define in the model
            onDelete: 'RESTRICT',
            onUpdate: 'CASCADE'
        });
    
  17. jony89 commented on Sep 19, 2021

    @jony89

    since it's been over than a year with this issue, def should be added in the documentation as a known issue under the "Upgrade to V6/V5" section. @papb

  18. github-actions commented on Nov 8, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

  19. TheOptimisticFactory commented on Nov 8, 2021

    @TheOptimisticFactory

    Don't mind me, I am just un-staling this issue, as it is still an issue

  20. yisibl commented on Dec 28, 2022

    @yisibl

    Hi everyone, any progress here?

  21. ephys commented on Dec 28, 2022

    @ephys
    Member

    That depends on what your issue is, since this issue regroups a bunch of them.

    I'm actually going to close this issue as we have no idea which bug has been fixed and which bug remains.

    If you're still experiencing an issue, please either comment in the relevant closed issue (listed in the first post of this thread) that you're still experiencing that issue and it should be re-opened, or open a new bug report.

  22. yevhen309 commented on Apr 20, 2023

    @yevhen309

    What a surprise for people who need to update some old projects with the newer version of sequelize... What if you have zillions of models and _ is used everywhere in the codebase (and it's not typescript). Do you have to search and fix it manually? This option is required for backwards compatibility.

  23. meruen commented on Aug 1, 2024

    @meruen

    This is very sad. 5 years now and still not fixed. I'm using "sequelize": "^6.37.3" and "sequelize-cli": "^6.6.2" and my timestamps are still in camelCase. I used sequelize-cli passing the field names in snake case plus underscored: true, but that's all. I was ready to migrate a big RoR application to JS using express+sequelize.

  24. mysuf commented on Oct 27, 2024

    @mysuf

    Not only the old projects. I use snake case in my javascript codebase on purpose to distinguish ORM object's names from rest of the code. Then it is obvious that those data came from/go to database and it is in line with the reality, so I don't have to change the variable names for any raw query replacements. Current state doesn't break anything, it just bloats the query with unnecessary aliases so it is true pain to read it while debugging. Such a simple fix which doesn't brake anything and yet ignored.

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

    P1: importantdocstype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions