Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

830
Views
Sequelize migrations - add foreign key constraint issue

I need to remove the existing foreign key constraint on a column and add a new constraint with same name which refers to primary key of another table.

I am getting an error ERROR: Constraint type must be specified through options.type. Though I am providing constraint type in option's object as third parameter.

Below is the migration code for reference.

 async up(queryInterface, Sequelize){
    const transaction = await queryInterface.sequelize.transaction();
    try {
      await queryInterface.removeConstraint(
        'shipments',
        'shipments_status_id_fkey',
        { transaction }
      );
      await queryInterface.addConstraint(
        'shipments',
        'status_id',
        {
          type: 'foreign key',
          name: 'shipments_status_id_fkey',
          references: {
            table: 'statuses',
            field: 'id'
          },
          transaction
        }
      );
      await transaction.commit();
    } catch (err) {
      await transaction.rollback();
      throw err;
    }
  }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Documentation here shows that options should be the second parameter to addConstraint function. https://sequelize.org/master/class/lib/dialects/abstract/query-interface.js~QueryInterface.html#instance-method-addConstraint

await queryInterface.addConstraint(
        'shipments',
        {
          type: 'foreign key',
          fields: ['status_id']
          name: 'shipments_status_id_fkey',
          references: {
            table: 'statuses',
            field: 'id'
          },
          transaction
        }
      );
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!