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.
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'
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With