Thomas Blanquet
Thomas Blanquet

Reputation: 507

Sequelize query singular instead of plural table name

I have an problem and I can't find anything that can solve it. I'm using sequelize and graphql to create an API in nodeJS. The database is using PostgresQL.

So I have two models: Simulation and Data. They are in two tables Simulations and Datas. The relation between them is one Simulation to many Datas.

The problem is this: when I make a query with Simulation (ex: Simulation.findAll()), it works correctly, querying "Simulations", but with Data, it queries on the "Data" table, not "Datas". What I really don't understand is that the code of my two models are almost the same.

Here is the model for Simulation:

module.exports = (sequelize, DataTypes) => {
  const Simulation = sequelize.define('Simulation', {
    id: {
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      type: DataTypes.INTEGER,
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
  });

  Simulation.associate = function(models) {
    Simulation.hasMany(models.Data, {
      foreignKey: 'SimulationId',
    })
  };

  return Simulation;
};

Here is the model for Data:

module.exports = (sequelize, DataTypes) => {
  const Data = sequelize.define('Data', {
    id: {
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      type: DataTypes.INTEGER,
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    content: {
      type: DataTypes.TEXT,
      allowNull: false
    },
    SimulationId: {
      allowNull: false,
      type: DataTypes.INTEGER,
    },
  });

  Data.associate = function(models) {
    Data.belongsTo(models.Simulation, {
      foreignKey: 'SimulationId',
      targetKey: 'id',
      allowNull: false,
      onDelete: 'CASCADE'
    });
  };
  return Data;
};

And here are the migration files:

Simulation

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Simulations', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      name: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Simulations');
  }
};

Data

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Datas', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      name: {
        type: Sequelize.STRING,
        allowNull: false
      },
      content: {
        type: Sequelize.TEXT,
        allowNull: false
      },
      SimulationId: {
        allowNull: false,
        type: Sequelize.INTEGER,
        onDelete: 'CASCADE',
        references: {
          model: 'Simulation',
          key: 'id',
          as: 'SimulationId',
        },
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Datas');
  }
};

Thanks for helping me :)

Upvotes: 2

Views: 4421

Answers (2)

Geet Choubey
Geet Choubey

Reputation: 1077

You can use freezeTableName option to set whatever the model name you want, sequelize will not make the model names plural.

Upvotes: 5

Roi
Roi

Reputation: 1003

Sequelize automatically makes the model names plural. Why not call the table "Data" It is actually a plural form of the word "Data", so maybe a better name for the table.

Upvotes: 1

Related Questions