New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ON DELETE constraint should default to CASCADE if foreignKey has allowNull: false #2831

Closed
mickhansen opened this Issue Jan 4, 2015 · 4 comments

Comments

5 participants
@mickhansen
Contributor

mickhansen commented Jan 4, 2015

var Foo = this.sequelize.define('Foo', { 
  bar_id: { type: Sequelize.INTEGER, allowNull: false }
});
var Bar = this.sequelize.define('Bar', {});

Foo.belongsTo(Bar, { foreignKey: 'bar_id'});

return Bar.sync({force: true}).then(function () {
  return Foo.sync({force: true});
});

Will fail on MySQL (haven't tested others so far) because the cascade defaults to SET NULL which "obviously" (took me a while to figure it out) obviously doesn't work well with a field that can't be NULL'ed.

Sequelize should check the foreignkey attribute definition, and if allowNull is false it should perhaps either use CASCADE by default or turn off constraints.

@EToreo

This comment has been minimized.

Contributor

EToreo commented Mar 10, 2015

A big +1 to this. This also took me a half day to figure out.

I might also be nice to have more options available to sync so that clients can turn the CASCADE and/or constraints off even when allowNull is set to true.

@mvallerie

This comment has been minimized.

mvallerie commented May 1, 2015

+1.

@phuvo

This comment has been minimized.

phuvo commented Nov 7, 2015

I'm using Sequelize 3.13 with PostgreSQL 9.4

var Foo = sequelize.define('Foo', {});
var Bar = sequelize.define('Bar', {});
Bar.belongsTo(Foo, {
    foreignKey: { allowNull: false },
});

but FooId doesn't have ON DELETE CASCADE.

@igordeoliveirasa

This comment has been minimized.

igordeoliveirasa commented Jul 24, 2017

mine is not working yet...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment