Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize fields must be specified through options.field error

So I created a table and modified it later on to add a foreign key. I followed this https://stackoverflow.com/a/47428160/12539780 but it gives me an error that says

"ERROR: Fields must be specified through options.fields"

I am using sequelize ^6.6.5 and my node version is 14.17.6 my code is provided below

module.exports = {
  up: async (queryInterface, Sequelize) =>
    Promise.all([
      await queryInterface.addColumn('bonus_transactions', 'wallet_id', {
        type: Sequelize.INTEGER.UNSIGNED,
        allowNull: false,
        after: 'order_id'
      }),
      await queryInterface.addConstraint('bonus_transactions', ['wallet_id'], {
        type: 'FOREIGN KEY',
        name: 'FK_bonus_transactions_wallet_id',
        references: {
          table: 'wallets',
          field: 'id'
        },
        onDelete: 'CASCADE'
      })
    ]),
  down: queryInterface => queryInterface.removeColumn('bonus_transactions', 'wallet_id')

could this be because of the sequelize version? I search through the v6 documentation and there doesn't seem to be any implementation that is similar to this.

like image 889
Jam Avatar asked Oct 24 '25 17:10

Jam


1 Answers

Fields in addConstaint should be indicated in fields option like this:

queryInterface.addConstraint('bonus_transactions',{
        type: 'FOREIGN KEY',
        name: 'FK_bonus_transactions_wallet_id',
        fields: ['wallet_id'], 
        references: {
          table: 'wallets',
          field: 'id'
        },
        onDelete: 'CASCADE'
      })
like image 184
Anatoly Avatar answered Oct 27 '25 08:10

Anatoly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!