Skip to content
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 · 5 comments

Comments

@mickhansen
Copy link
Contributor

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
Copy link
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
Copy link

+1.

@phuvo
Copy link

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
Copy link

mine is not working yet...

@Mageek627
Copy link

Mageek627 commented Aug 6, 2020

@phuvo It's because you use belongsTo instead of hasOne/hasMany. They seem to have different behaviors with the ON DELETE constraint. See the description of this PR: #4506

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

No branches or pull requests

6 participants