Skip to content

Could not create constraint or index. See previous errors. #724

Description

@v1d3rm3

Additional info: If the table that will be referenced by a table of migration already exists, then sequelize create foreign key successfuly, but if the table that will be referenced by a table created in migration before, then it not works.

What you are doing?

'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    return await queryInterface.createTable(
      'Comprovante',
      {
        IdComprovante: {
          type: Sequelize.INTEGER,
          primaryKey: true,
          autoIncrement: true,
        },
        Descricao: {
          type: Sequelize.STRING,
          allowNull: false,
        },
        Valor: {
          type: Sequelize.DECIMAL(10, 2),
          allowNull: false,
          comment:
            'O valor do documento ou da parte do documento que ' +
            'servirá de comprovação',
        },
        AtualizadoEm: {
          type: Sequelize.DATE,
          allowNull: true,
        },
        CriadoEm: {
          type: Sequelize.DATE,
          allowNull: false,
          defaultValue: Sequelize.NOW,
          comment:
            'Este campo é importante para indicar a data que vai ' +
            'ter início para computar o auxílio',
        },
        DboIdDocumento: {
          type: Sequelize.INTEGER,
          comment: 'Referência ao documento digitalizado',
        },
      },
      { schema: 'auxilio_saude_v2' },
    );
  },

  down: async (queryInterface, Sequelize) => {
    return await queryInterface.dropTable('Comprovante');
  },
};
'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    return await queryInterface.createTable(
      'Ressarcimento',
      {
        IdRessarcimento: {
          type: Sequelize.INTEGER,
          primaryKey: true,
          autoIncrement: true,
        },
        Tipo: {
          type: Sequelize.ENUM('recorrente', 'nao-recorrente'),
          allowNull: false,
        },
        Descricao: {
          type: Sequelize.STRING,
          allowNull: false,
        },
        MesCompetencia: {
          type: Sequelize.INTEGER,
          allowNull: false,
          comment: 'A partir de/Em qual mês o ressarcimento valerá',
        },
        AnoCompetencia: {
          type: Sequelize.INTEGER,
          allowNull: false,
          comment: 'A partir de/Em qual ano o o ressarcimento valerá',
        },
        Valor: {
          type: Sequelize.DECIMAL(10, 2),
          allowNull: false,
          comment:
            'A atualização em valor gerará um novo ressarcimento ' +
            '(se ele for recorrente)',
        },
        Ativo: {
          type: Sequelize.BOOLEAN,
          allowNull: false,
        },
        MotivoNaoAtivo: {
          type: Sequelize.STRING,
          allowNull: true,
        },
        AtualizadoEm: {
          type: Sequelize.DATE,
          allowNull: true,
        },
        CriadoEm: {
          type: Sequelize.DATE,
          allowNull: false,
          defaultValue: Sequelize.NOW,
          comment:
            'Este campo é importante para indicar a data que vai ' +
            'ter início para computar o auxílio',
        },

        /** Foreign keys */
        IdComprovante: {
          type: Sequelize.INTEGER,
          references: {
            model: 'Comprovante',
            key: 'IdComprovante',
            schema: 'auxilio_saude_v2',
          },
        },
        DboIdVinculo: {
          type: Sequelize.INTEGER,
          references: {
            model: 'Vinculo',
            key: 'IdVinculo',
            schema: 'dbo',
          },
        },
      },
      { schema: 'auxilio_saude_v2' },
    );
  },

  down: async (queryInterface, Sequelize) => {
    return await queryInterface.dropTable('Ressarcimento');
  },
};

What do you expect to happen?

To create a foreign key in Ressarcimento.IdComprovante. If i comment

references: {
            model: 'Comprovante',
            key: 'IdComprovante',
            schema: 'auxilio_saude_v2',
          },

then, it works.

What is actually happening?

== 20181205154655-CreateAuxilioSaudeSchema: migrating =======
== 20181205154655-CreateAuxilioSaudeSchema: migrated (0.031s)

== 20181205154700-CreateAuxilioSaudeComprovanteTable: migrating =======
== 20181205154700-CreateAuxilioSaudeComprovanteTable: migrated (0.037s)

== 20181205155631-CreateAuxilioSaudeRessarcimentoTable: migrating =======

ERROR: Could not create constraint or index. See previous errors.

Dialect: MSSQL
Sequelize CLI version: latest
Sequelize version: latest

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions