Ad

Adding Associations To Non Default Schema With Sequelize Migrations

I am trying to create a sequelize migration that adds an association to a table that is under a non default schema. Here is the migration code I am trying to use:

module.exports = {
  up: (queryInterface, Sequelize) =>
    queryInterface
      .addColumn(
        'Users',
        'profileId',
        {
          type: Sequelize.INTEGER,
          references: {
            model: {
              tableName: 'Profile',
              schema: 'annotations',
            },
            key: 'id',
          },
          allowNull: false,
          onUpdate: 'CASCADE',
          onDelete: 'SET NULL',
        },
        {
          schema: 'annotations',
        },
      ),

  down: queryInterface =>
    queryInterface
      .removeColumn('Users', 'profileId', {
        schema: 'annotations',
      }),
};

Im getting the following error: ERROR: relation "public.Users" does not exist

How can I get sequelize to go to "annotations.Users" instead of "public.Users"?

Ad

Answer

This is an issue with sequelize which adds "public." to a table creation by default.

One option you have is to use sequelize queryInterface to create the schema and the table and for the associations, use queryInterface.sequelize.query with a raw query.

There's also a pull request that supports non default schemas in migration: https://github.com/sequelize/sequelize/pull/11274.

Ad
source: stackoverflow.com
Ad