0

I would like to add a constraint inside my migration file, for example when I try and delete a row and there's another row inside another table that's referencing the row i'm deleting it needs to throw an error. There will also be multiple tables that will associate with the table.

return queryInterface.createTable('status', {
  id: {
    allowNull: false,
    autoIncrement: true,
    primaryKey: true,
    type: Sequelize.INTEGER
  },
  name: {
    type: Sequelize.STRING,
    unique: true
  },
  slug: {
    type: Sequelize.STRING,
    unique: true
  },
  createdAt: {
    allowNull: false,
    type: Sequelize.DATE
  },
  updatedAt: {
    allowNull: false,
    type: Sequelize.DATE
  },
});

table status: ... id

table locales: ... id_status

table users ... id_status

user990717
  • 470
  • 9
  • 18

2 Answers2

0
  id_status: {
    type: Sequelize.INTEGER,
    references: {
      model: 'status',
      key: 'id',
      onDelete: 'restrict'    
    }
  },
user990717
  • 470
  • 9
  • 18
0
 id_status: {
    type: Sequelize.INTEGER,
    references: {
      model: 'status',
      key: 'id',
    },
onDelete: 'restrict'    
  },
margherita pizza
  • 6,623
  • 23
  • 84
  • 152