sequelize: Several bugs/questions regarding underscored/underscoredAll in Sequelize v5

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!

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 25
  • Comments: 31 (10 by maintainers)

Commits related to this issue

Most upvoted comments

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

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 👍

I just stopped using Sequelize, as this is kind of a breaking feature for me.

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.

An issue like this should be priority number one, I’m sure a lot of people are used to using underscore for column and table names in a database. It has been almost an year since this issue was opened, I’know its an open source project and that its hard for maintainers to find the time, but we at least need an update of the progress on this and a rough ETA.

I greatly appreciate the work you guys have done with sequelize, its a wonderful library, but please gives us more than “no updates on this”, its an important feature that has been broken for a long time now.

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.

By the way sorry guys I am taking long to look at this. Haven’t had much time, thanks for the patience, stay tuned! Also rest assured that I will not consider this ‘done’ until every “sub-issue” listed in the first post is properly addressed.

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.

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!

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

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

[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' });
  }
};

IMO these issues are stemmed from lack of documentation for changes in underscored system in v5. This comment explains how this new approach works.

I understand some dev just want to use snake case for everything, there is a solution for that (in comment linked) but a bit more verbose than v4/v3.