sequelize migration foreign key array code example

Example 1: sequelize migration add column foreign key

queryInterface.addConstraint('table_name', {
  fields: ['column_name'],
  type: 'foreign key',
  name: 'custom_fkey_constraint_name', // optional
  references: {
    table: 'target_table_name',
    field: 'target_column_name'
  },
  onDelete: 'cascade',
  onUpdate: 'cascade'
});

Example 2: sequelize create record with foreign key constraint

Project.belongsTo(User, {foreignKey: 'user_id'})

Example 3: ignore foreign key constraint in sequelize

UnhandledPromiseRejectionWarning: SequelizeForeignKeyConstraintError: insert or update on table "approvers" violates foreign key constraint