Roi Roi -4 years ago 147
Javascript Question

Prevent sequelize adding ending lowercase 's'

I have this table defined as


And I have configured it with this, to freeze the table name

freezeTableName: true

And I use this table as an include query in another table like this

request.models.T_CPCORE_INGREDIENTS.findById(, {
include: [
model: request.models.T_CPCORE_INGREDIENT_UNITS

But if I print the object I get returned I can see in the console that the T_CPCORE_INGREDIENT_UNITS now looks like this


With a lowercase 's' at the end. And this is very annoying, because sometimes in my code where I use this name have to remember to use the lowercase 's'.
How can I prevent this?

Answer Source

You need to use alias when defining association. If not, Sequelize automatically adds the unwanted s at the end of associated model name (in case of 1:m or n:m relations). Let's assume hasMany relation, just to show how it looks like


alias (as) defines plural name of associated model. Important thing when defining alias is to use it when eager loading models while querying

request.models.T_CPCORE_INGREDIENTS.findById(, {
    include: [
            model: request.models.T_CPCORE_INGREDIENT_UNITS,

So now, the model would have property T_CPCORE_INGREDIENT_UNITS as an array, without the added s at the end.

Note: alias can also be defined as object having properties singular and plural, which would define the naming strategy for single association or multiple associations.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download