最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

偏执表上的级联删除

IT培训 admin 12浏览 0评论

偏执表上的级联删除

我在两个偏执表(用户和电子邮件)之间有一个关系,并且在关系上有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

偏执表上的级联删除

我在两个偏执表(用户和电子邮件)之间有一个关系,并且在关系上有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

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论