I have a relationship between two paranoid tables (User and Email) and I have the onDelete: 'cascade' option on the relationship ( Email.belongsTo(User, onDelete:'cascade') ).
The problem is that when I delete the user his email is not being deleted by cascade.
Did I make a mistake or there is bug on sequelize?
I am using sequelize 2.0.0-rc2 on a postgres database.
Thanks.
PS.: Please take a look at the code I used for test:
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");
}
});
});