sequelize / sequelize/sequelize

Support adding a NOT VALID to foreign key constraint in Postgres

Open
#13,905 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

P4: nice to have
Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

Issue Creation Checklist

Feature Description

Postgres has the ability to add NOT VALID to a foreign key constraint. When added, it skips the long check on a table. In certain cases this can be a desirable and more reliable way of adding constraints on large production databases.

It'd be great if there was an easy and nice way to support this using the ORM DSL, instead of having to write raw SQL to accomplish the task.

Is your feature request related to a problem? Please describe.

No

Describe the solution you'd like

It'd be great if there was an easy and nice way to support this using the ORM DSL, instead of having to write raw SQL to accomplish the task.

Example: Introducing a notValid: true key in the options object. Default being false, to preserve existing behavior.

queryInterface.addConstraint('Posts', {
  fields: ['username'],
  type: 'foreign key',
  name: 'custom_fkey_constraint_name',
  references: { //Required field
    table: 'target_table_name',
    field: 'target_column_name'
  },
  notValid: true,
});
Why should this be in Sequelize

Since the query being generated is from sequelize query interface, it perhaps makes sense for this to be in sequelize package.

Describe alternatives/workarounds you've considered

Writing raw SQL statements.

Additional context

This feature is geared more towards giving developers simpler options to write more reliable SQL statements when working on large production databases (albeit with tradeoffs). In similar vein as https://github.com/sequelize/sequelize/issues/13901. Having this would mean that developers can use the DSL over raw SQL, making it a good dev UX.

Feature Request Checklist

Is this feature dialect-specific?
  • No. This feature is relevant to Sequelize as a whole.
  • Yes. This feature only applies to the following dialect(s): Postgres
Would you be willing to implement this feature by submitting a Pull Request?
  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

I am more than happy to work on this and create PR if/when the feature is approved. Thank you!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the QueryInterface.addConstraint entry point and trace how PostgreSQL foreign-key constraints are generated. Add support for the proposed notValid option, preserving the default behavior when it is false, and verify that the PostgreSQL SQL includes NOT VALID when enabled.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, typescript
Domain
databases
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.