title: UnhandledPromiseRejectionWarning: SequelizeUnknownConstraintError: Unknown constraint error
body: <p>I am working with an API in NodeJs and Express to connect with a PostgreSQL database using Sequelize. When I run my program in the end of the Sequelize output in the terminal i got:</p>
  <pre><code>Executing (default): ALTER TABLE &quot;installation_sites&quot; DROP CONSTRAINT &quot;installation_sites_contractor_id_fkey&quot;
  (node:21312) UnhandledPromiseRejectionWarning: SequelizeUnknownConstraintError: Unknown constraint error
      at Query.formatError (C:\Users\caleb\OneDrive\Documentos\Aplicativo_RDO\API\node_modules\sequelize\lib\dialects\
  postgres\query.js:376:17)
      at Query.run (C:\Users\caleb\OneDrive\Documentos\Aplicativo_RDO\API\node_modules\sequelize\lib\dialects\
  postgres\query.js:87:18)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)
  Executing (default): ALTER TABLE &quot;installation_sites&quot; ALTER COLUMN &quot;contractor_id&quot; SET NOT NULL;
  ALTER TABLE &quot;installation_sites&quot;  ADD FOREIGN KEY (&quot;contractor_id&quot;) REFERENCES &quot;contractors&quot; (&quot;contractor_id&quot;) 
  ON DELETE NO ACTION ON UPDATE CASCADE;
  (node:21312) UnhandledPromiseRejectionWarning: SequelizeUnknownConstraintError: Unknown constraint error
      at Query.formatError (C:\Users\caleb\OneDrive\Documentos\Aplicativo_RDO\API\node_modules\sequelize\lib\
  dialects\postgres\query.js:376:17)
      at Query.run (C:\Users\caleb\OneDrive\Documentos\Aplicativo_RDO\API\node_modules\sequelize\lib\
  dialects\postgres\query.js:87:18)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)
  (node:21312) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 
  This error originated either by throwing inside of an async function without a catch block, 
  or by rejecting a promise which was not handled with .catch(). To terminate the node process 
  on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` 
  (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
  (node:21312) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. 
  In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  </code></pre>
  <p>I don't understand what is causing this. Here are my models involved:</p>
  <p>InstallationSite.js model:</p>
  <pre><code>const Sequelize = require(&quot;sequelize&quot;);
  const sequelize = require(&quot;../database/index&quot;);
  const Contractor = require(&quot;../models/Contractor&quot;);
  const JobPosition = require(&quot;./JobPosition&quot;);
  
  class InstallationSite extends Sequelize.Model {}
  
  InstallationSite.init(
    {
      installation_site_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true,
      },
      installation_site_status: {
        type:Sequelize.STRING,
        allowNull:false,
        validate:{
          isIn:[['Em Andamento', &quot;Finalizada&quot;, &quot;Não Iniciada&quot;]]
        }
      },
      enterprise_name: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      enterprise_start_date: {
        type: Sequelize.DATEONLY,
        allowNull: false,
      },
      enterprise_duration_days: {
        type: Sequelize.INTEGER,
        allowNull: false,
      },
      enterprise_city: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      enterprise_state: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      contractor_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: &quot;contractors&quot;,
          key: &quot;contractor_id&quot;,
        },
      },
    },
    {
      sequelize,
      timestamps: false,
      tableName: &quot;installation_sites&quot;,
    }
  );
  
  InstallationSite.belongsTo(Contractor, {
    foreignKey: &quot;contractor_id&quot;,
    targetKey: &quot;contractor_id&quot;,
  });
  Contractor.hasMany(InstallationSite, {
    foreignKey: &quot;contractor_id&quot;,
    sourceKey: &quot;contractor_id&quot;,
  });
  
  InstallationSite.belongsToMany(JobPosition, {
    through: &quot;contracts&quot;,
    foreignKey: &quot;installation_sites_intallation_site_id&quot;,
    as: &quot;positions&quot;,
  });
  
  JobPosition.belongsToMany(InstallationSite, {
    through: &quot;contracts&quot;,
    foreignKey: &quot;job_positions_job_position_id&quot;,
    as: &quot;installations&quot;,
  });
  
  sequelize.sync({ alter: true });
  module.exports = InstallationSite;
  </code></pre>
  <p>Contractor.js model:</p>
  <pre><code>const Sequelize = require(&quot;sequelize&quot;);
  const sequelize = require(&quot;../database/index&quot;);
  
  class Contractor extends Sequelize.Model {}
  
  Contractor.init(
    {
      contractor_id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false,
      },
      contractor_name: {
        type: Sequelize.STRING,
        validate: {
          is: {
            args: /[a-zA-z]/,
            msg: &quot;Contractor name must contain only letters&quot;,
          },
          len: {
            args: [5, 50],
            msg:
              &quot;Contractor name must contain at least five letters and less than 50 letters&quot;,
          },
          notEmpty: {
            args: true,
            msg: &quot;Contractor name is not optional&quot;,
          },
        },
      },
    },
    {
      sequelize,
      timestamps: false,
      tableName: &quot;contractors&quot;,
    }
  );
  
  sequelize.sync({ alter: true });
  module.exports = Contractor;
  </code></pre>
  <p>I would like to know if anyone can help me find out what is causing this warning.</p>
  <p>[EDIT] The constraint <code>installation_sites_contractor_id_fkey</code> seems to exist inside the DB.</p>
  <p><a href="https://i.stack.imgur.com/0KZeC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0KZeC.png" alt="Picture of DB content" /></a></p>
  <p>[EDIT2]</p>
  <p>After a manually executed the command
  <code>ALTER TABLE &quot;installation_sites&quot; DROP CONSTRAINT &quot;installation_sites_contractor_id_fkey&quot;</code> initially it couldn't find the constraint but i ran again and the answer was <code>found null row</code> and the constraint was gone.
  I ran again my server and several repeated keys of installation_sites_contractor_id_fkey were created and then sequelize started complaining about others fkeys. I still can`t comprehend this behaviour</p>