How To Avoid Pluralization of Table Names In Sequelize.

You might be having problem with sequelize automatically pluralizing your table names in your model. This article will help you fix it.

Sometimes you already have your table created in your database already and you want to write a model for it on your node project. Assume you have a table named "school_info" in your database with the following columns "matric_no", "first_name", "last_name", "createdAt", "updatedAt", 'school".
And you write a model like the below for your table.

const { DataTypes } = require('sequelize');
const sequelize = require('../src/connection/db'); 

const SchoolInfo = sequelize.define('school_info', {
  matric_no: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true,
  },
  first_name: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  last_name: {
    type: DataTypes.STRING,
    allowNull: false,
  },

  createdAt: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
  },
  updatedAt: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
  },
  school:{
    type: DataTypes.STRING(50),
    allowNull: false,
  },


});

SchoolInfo.belongsTo(require('./Students'), {
  foreignKey: 'matric_no',
  targetKey: 'matric_no',
  as: 'student', 
});


module.exports = SchoolInfo;

You are optimistic everything is correct and expects your code to run fine, but you keep getting errors like the below.

parent: error: relation "school_infos" does not exist

In my case, I was wondering where I had 'school_infos' instead of 'school_info'. I searched my entire project and there was no single instance of 'school_infos'. I kept wondering until I stumbled on the 'pluralization' concept in sequelize.
I figured out that on compiling, sequelize is converting my singular 'school_info' table name to 'school_infos'. I really didn't know why it was happening since in the sequelize naming documentation, it was recommended to use singular table names.
But I wanted to maintain my table name as 'school_info' so I updated the model code to this

const { DataTypes } = require('sequelize');
const sequelize = require('../src/connection/db'); 

const SchoolInfo = sequelize.define('school_info', {
  matric_no: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true,
  },
  first_name: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  last_name: {
    type: DataTypes.STRING,
    allowNull: false,
  },

  createdAt: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
  },
  updatedAt: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
  },
  school:{
    type: DataTypes.STRING(50),
    allowNull: false,
  },
},{
    freezeTableName: true, // Prevent automatic pluralization of the table name
  });

SchoolInfo.belongsTo(require('./Students'), {
  foreignKey: 'matric_no',
  targetKey: 'matric_no',
  as: 'student', 
});


module.exports = SchoolInfo;

By adding the { freezeTableName: true, } to the code, I was able to prevent automatic pluralization of the table name. I hope this helps you.