偏执表上的级联删除 - Sequelize

lao*_*lao 0 postgresql node.js sequelize.js

我在两个偏执表(用户和电子邮件)之间有一个关系,并且该关系上有 onDelete: 'cascade' 选项( Email.belongsTo(User, onDelete:'cascade') )。

问题是,当我删除用户时,他的电子邮件不会级联删除。

是我写错了还是sequelize有bug?

我在postgres数据库上使用sequelize 2.0.0-rc2

谢谢。

PS:请看一下我用于测试的代码:

var Sequelize = require('sequelize')
    , sequelize = new Sequelize('test', 'test', 'test', {dialect: 'postgres'});

var User = sequelize.define('User', {
    username: Sequelize.STRING,
    birthday: Sequelize.DATE
}, {paranoid: true}); //Without paranoid the code works fine

var Email = sequelize.define('Email', {
    email: Sequelize.STRING,
    primary: Sequelize.BOOLEAN
}, {paranoid: true}); //Without paranoid the code works fine

Email.belongsTo(User, {onDelete: 'cascade'});

sequelize.sync().success(function () {
    var userCreatedInstance = null;

    var deletedUserId = null;
    var cascadeDeletedEmailId = null;

    User
        .create({
            username: 'user1',
            birthday: new Date(1986, 06, 28)
        })
        .then(function (_user) {
            userCreatedInstance = _user;
            deletedUserId = userCreatedInstance.id;
            return Email.create({email: 'email1@test.com', primary: true, UserId: userCreatedInstance.id});
        })
        .then(function (createdEmail) {
            cascadeDeletedEmailId = createdEmail.id;
            return userCreatedInstance.destroy();
        })
        .then(function () {
            return User.find(deletedUserId);
        })
        .then(function (foundUser) {
            if(foundUser){
                throw new Error("Shouldn't find the user with this id");
            }
            return Email.find(cascadeDeletedEmailId);
        })
        .then(function (foundEmail){
            if(foundEmail){
                console.log(foundEmail.values);
                throw new Error("Shouldn't find the email with this id");
            }
        });
});
Run Code Online (Sandbox Code Playgroud)

Sod*_*pha 6

如果你想让级联在偏执狂身上工作,你将不得不使用钩子。请在下面找到我是如何解决的

我在父表(关系的基础)上添加了 afterDestroy 钩子,使其获得与 with 关联的模型并在其上调用 destroy 方法。就这么简单!我相信我们可以让它以这种方式工作,直到维护者给我们另一种方法

// User Model
const User = sequelize.define('users', {....},
{
        tableName: 'users',
        deletedAt: 'deletedAt',
        paranoid: true,
        timestamps: true,
        hooks: {
            afterDestroy: function (instance, options) {
                instance.getProduct().then(product=> product.destroy()); // Softdelete on product table
                console.log('after destroy: destroyed');
            }
        }
    }
);

// Product Model
const Product = sequelize.define('products', {....},
{
        tableName: 'products',
        deletedAt: 'deletedAt',
        paranoid: true,
        timestamps: true,
    }
)

// User relationship with product
User.hasOne(Product,
    {
        as: 'Product',
        onDelete: 'CASCADE',
        hooks: true,
        foreignKey: "userID"
    })
Product.belongsTo(User, {
    as: 'User',
    foreignKey: "userID"
});
Run Code Online (Sandbox Code Playgroud)