Sunday 19 March 2017

Sequelize: How to enable onDelete: cascade?

Sequelize Version: 3.24.3

I'm trying to enable cascade deletion on an association in Sequelize.

My data model looks like:

// models/pin.js
Pin.belongsTo(models.category, { foreignKey: 'categoryId' });

// models/category.js
Category.hasMany(models.pin, { foreignKey: 'categoryId' });

I've tried every permutation of adding onDelete: 'cascade, hooks:true to just the belongsTo options, just the hasMany options, and then both options.

This post suggested just adding those flags to the belongsTo association (the docs suggest adding it to the hasMany). When I trigger a delete, I'm receiving the foreign key constraint error:

SequelizeForeignKeyConstraintError: update or delete on table "categories" violates foreign key constraint "pins_category_id_fkey" on table "pins"



via kuiro5

No comments:

Post a Comment