U
U
uib2018-11-01 07:15:23
Node.js
uib, 2018-11-01 07:15:23

How to delete related data?

I am using Sequelize ORM. And I want to make sure that all attached data is deleted. But at the moment, information is deleted only from the first table, and only the foreign key is deleted from the table linked by id, while the information itself remains.

const db = require('../config/db.config.js')
const SprWellplatforms = db.spr_wellplatforms
const SprOilfield = db.spr_oilfields
const errorHandler = require('../utils/errorHandler')

module.exports.remove = async function(req, res) {
    try {
        await SprOilfield.destroy({
            where : {
                oilfield_id: req.params.id
            }
        })
        await SprWellplatforms.destroy({
            where: {
                spr_oilfields_id: req.params.id //spr_oilfields_id - это foreign key
            }
        })
        res.status(200).json({
            message: 'Данные удалены.'
        })  
    } catch(e) {
        errorHandler(res, e)
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Decadal, 2018-11-01
@uib

Read this section carefully: docs.sequelizejs.com/class/lib/associations/base.j...
You need to find on delete cascade. Your links apparently have an option selected that results in on delete set NULL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question