3

While using Sequelize and reading the Sequelize docs, I observed that sometimes model names are used in singular and sometimes in plural. Some methods automatically added to models by associations have the singular form and some have the plural form.

1. How does Sequelize compute the plurals? Does it simply append an "s" to the string?

2. What if I want to use a noun with an irregular plural, such as "Person"?

3. When defining an instance, should I use the singular or the plural form?

4. When defining an alias, should I use the singular or the plural form?

5. When defining a Many-to-many relationship, should I use the singular or the plural form in the through option?

1 Answer 1

15

How Sequelize computes plurals

Under the hood, sequelize uses an awesome library called inflection to compute singular and plural forms of words, which works with virtually any english word, including irregular plurals such as "Person" -> "People", "Octopus" -> "Octopi", "Tooth" -> "Teeth". There are a few words that inflection gets wrong though, and for those cases, or cases in which you want to force your custom singular/plural words, see the next topic.


What if I want to use a noun with an irregular plural?

As said above, most words with irregular plurals will be pluralized correctly by inflection (including "Person"). But there are a few words that inflection gets wrong. Sequelize allows you to force the singular and plural forms you wish, usually accepting a plain object with the form { singular: "your-singular-here", plural: "your-plural-here" } in place of the name string. See the next topics for more details.


When defining an instance

When defining an instance, the standard way is to use the singular name, but you can also use the plural name, since sequelize will apply inflection methods on it to get the singular and plural form whenever needed:

const Foo = sequelize.define("foo", {
    // attributes
});

If you want to force your custom singular and plural forms, use the third parameter to specify the name option:

const Foo = sequelize.define("foo", {
    // attributes
}, {
    name: {
        singular: "mycustomsingularstring",
        plural: "mycustompluralstring"
    }
});

When defining an alias

When defining an alias for a hasOne or belongsTo association, you must use the singular form:

Foo.belongsTo(Bar, { as: "person" });

When defining an alias for a hasMany or belongsToMany association, you must use the plural form:

Foo.belongsToMany(Bar, { through: Foo_Bar, as: "people" });

Or, if you want to force your custom singular/plural strings:

Foo.belongsToMany(Bar, {
    through: Foo_Bar,
    as: {
        singular: "mycustomsingularstring",
        plural: "mycustompluralstring"
    }
});

When defining a Many-to-many relationship

If you have a sequelize model for the junction table itself, the best practice is to use the model itself in the through option:

// If you have this somewhere
const Foo_Bar = sequelize.define("foo_bar", {
    // attributes
});

// Then the best practice is to pass the model itself
Foo.belongsToMany(Bar, { through: Foo_Bar });

If you don't have a sequelize model for the junction table itself, which is not unusual (you are letting sequelize do the many-to-many magic by itself), then you must use the plural form:

Foo.belongsToMany(Bar, { through: "foo_bars" });

By the way, you might have noticed that all model definitions in my examples are lowercase, this is to follow a convention from PostgreSQL (if you have uppercase letters in your tables in PostgreSQL you might have problems), but if you're not using PostgreSQL you're probably fine to use uppercase characters in your model names.

9
  • 1
    to be frank they should honestly have not added this, why do they want to make decisions on behalf of the user what the tablename would be called, this has complicated maintenance of their lib
    – PirateApp
    Sep 18, 2019 at 6:07
  • 2
    @PirateApp Hello, I've become a maintainer now, in the future I might start some changes to move towards changing this default behavior!
    – Pedro A
    Sep 18, 2019 at 11:42
  • Im really getting irritated with this singular and plural thing and its confusing as to where which has to be used. Its making the entire usage cumbersome and waste of time
    – Shyam R
    Dec 1, 2020 at 12:35
  • @ShyamSundarR Hi, sorry to hear that you're having trouble, thank you for letting me know, perhaps we can add an option to disable this altogether. However, ideally we would want plurals to be applied on places where it does make sense, of course we don't want peopke's time to be wasted, can you please open an issue showing a minimal example in which this bothered you? Thanks!
    – Pedro A
    Dec 1, 2020 at 12:38
  • 1
    @PedroA, Im a very new person trying to get around this package very recently maybe about a week back. I basically was trying to look into github's existing packages for some sample code. I generally know the general db principles and have worked with mongoose and mongo. I mean Im basically confused at places where we define models, the migrations createtable method. Multiple places are there. I also never could really understand when the models js files will be used, , will it be internally used to restrain data which are migrated to those models? really not interested to go through full docs
    – Shyam R
    Dec 1, 2020 at 13:28

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.