0
I created a web application using sequelize, my model looked like this:
const Product = db.sequelize.define('product', {
idProduct: {
type: db.Sequelize.STRING,
primaryKey: true,
autoIncrement: false
},
name: {
type: db.Sequelize.STRING
},
idCategory: {
type: Sequelize.STRING,
references: {
model: Category,
key: 'idCategory',
}
}
})
I can register normally by entering the id, name and id of the foreign key, but when I try to delete the record returns a Constraint error because of the foreign key.
My code to delete:
app.post('/produto', (req, res) => {
Product.destroy({ where: { idProduct: req.body.id } }).then(() => {
res.redirect('/produto')
}).catch((err) => {
res.redirect('/')
})
})
How can I delete a record that references another table in the sequelize?