偏执表上的级联删除

编程入门 行业动态 更新时间:2024-10-07 10:16:20

<a href=https://www.elefans.com/category/jswz/34/1727952.html style=偏执表上的级联删除"/>

偏执表上的级联删除

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

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

我做错了还是续集上有错误?

我正在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: '[email protected]', 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");
            }
        });
});
回答如下:

我发现级联选项仅设置数据库级联选项。没有通过级联进行级联删除。

但是续集团队将来将为偏执表进行级联工作。

对此存在一个未解决的问题:https://github/sequelize/sequelize/issues/2586

更多推荐

偏执表上的级联删除

本文发布于:2024-05-07 04:52:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754328.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:偏执   表上   级联

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!