Sequelize: `underscored: true` is not working

Created on 20 Feb 2016  路  1Comment  路  Source: sequelize/sequelize

Hello!

My model attribute is defined like this:

firstName: {
  type: Sequelize.STRING(60)
},

Sequelize instance is configured with: underscored: true and underscoredAll: true.

However, when I run the app it throws an error: column "firstName" does not exist.

I'm using pre-existing database without the sync feature. All table and column names are in the snake case, e.g. users.first_name. Is is possible to have my database in snake_case, but properties in my application in camelCase without manually specifying the tableName and field options?

Also I would like createdAt and updatedAt properties to be automatically added to my model, but by defined like created_at and updated_at in the database. Is it also possible? I can't find a way to do this without re-defining each field for all the models manually using field option.

Thank you!

Most helpful comment

The underscored option only handles the columns generated by sequelize - resulting in created_at, user_id etc. - You need to use the field option to achieve what you want.

If you want it to apply to all your tables you could do so in a beforeDefine hook

sequelize.beforeDefine(function (attributes, options) {
  // Loops through attributes and sets field
});

>All comments

The underscored option only handles the columns generated by sequelize - resulting in created_at, user_id etc. - You need to use the field option to achieve what you want.

If you want it to apply to all your tables you could do so in a beforeDefine hook

sequelize.beforeDefine(function (attributes, options) {
  // Loops through attributes and sets field
});
Was this page helpful?
0 / 5 - 0 ratings