question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change default referential action

See original GitHub issue

Problem Associations (foreign keys) in Sequelize define a rather opinionated default referential action for ON UPDATE / ON DELETE clauses.

For ON UPDATE, Sequelize defaults to CASCADE. For ON DELETE, Sequelize defaults to SET NULL if the source field allows null values; otherwise, CASCADE is the default.

It would be convenient to set RESTRICT or NO ACTION as the default (database-wide) in some circumstances. In MySQL, for example, the default referential action is RESTRICT when not explicitly specified; Sequelizes seems to override these defaults.

Describe the solution you’d like It would be nice to override the “default” referential action for ON UPDATE and ON DELETE when associations are created. It can get annoying to override this behavior for every single association in the database if RESTRICT is the general rule / policy. This could be done in the options Object passed to the new Sequelize instance (see usage example below).

Why should this be in Sequelize This is a small change (probably ~10 lines of code), but it gives extra flexibility to the developer without requiring the developer to be verbose with association definitions.

Describe alternatives you’ve considered Another possible solution is creating some sort of hook that allows one to change the default onDelete and onUpdate options of an association. I don’t know of any such hook at this time.

Usage example

const db = new Sequelize(config.database, config.username, config.password, {
	host: config.host,
	// New `associations` key
	associations: {
		onDelete: 'RESTRICT',
		onUpdate: 'CASCADE',
	}
})

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ephyscommented, Apr 21, 2022

We discussed whether to accept this feature request at today’s meeting. We did not feel it was necessary to implement this, as this is already achievable with hooks, and we felt the demand was too low for the extra complexity.

Example of how this can be handled with hooks:

sequelize.afterDefine(model => {
  model.beforeAssociate((data, options) => {
    options.onDelete ||= 'NO ACTION';
  });
});

It’s unfortunately a bit verbose, but you only need to write it once.

We can revisit this at a later date if there is enough demand for it.

0reactions
bminercommented, Aug 5, 2022

Also, thanks to you, I just learned that ||= is a thing in JavaScript.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Referential actions - Prisma
Referential actions let you define the update and delete behavior of related models on ... You can define tables using the SET DEFAULT...
Read more >
Referential Constraint Action Options
Referential Constraint Actions are the actions that the database takes in the event that an attempt is made to delete or update a...
Read more >
Referential actions
Referential integrity actions sometimes allow changes to a table that temporarily violate a referential constraint. The NO ACTION option allows such violations.
Read more >
Change the Default Referential Integrity Settings ... - erwin, Inc.
You can use the Model Properties dialog to change the default referential integrity settings for all relationships. ... Click Model Properties on the...
Read more >
FOREIGN KEY Referential Actions in MySQL - with examples.
NO ACTION – In MySQL means the same as RESTRICT . This wording comes from Standard SQL. SET DEFAULT – This syntax is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found