【发布时间】:2015-01-14 23:53:15
【问题描述】:
我有两个偏执表(用户和电子邮件)之间的关系,并且我在关系上有 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");
}
});
});
【问题讨论】:
标签: node.js postgresql sequelize.js