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

Allow setting the ON DELETE behaviour of foreign key constraints #297

Merged
merged 8 commits into from
Mar 1, 2024

Conversation

andrew-farries
Copy link
Collaborator

Add support for setting the ON DELETE behaviour of a foreign key constraint.

An example migration that uses the behaviour is:

{
  "name": "21_add_foreign_key_constraint",
  "operations": [
    {
      "alter_column": {
        "table": "posts",
        "column": "user_id",
        "references": {
          "name": "fk_users_id",
          "table": "users",
          "column": "id",
          "on_delete": "CASCADE"
        },
        "up": "(SELECT CASE WHEN EXISTS (SELECT 1 FROM users WHERE users.id = user_id) THEN user_id ELSE NULL END)",
        "down": "user_id"
      }
    }
  ]
}

The valid options for on_delete are CASCADE, SET NULL, RESTRICT, or NO ACTION. If the field is omitted, the default is NO ACTION,

Fixes #221

@andrew-farries andrew-farries merged commit def08e2 into main Mar 1, 2024
42 checks passed
@andrew-farries andrew-farries deleted the foreign-key-options branch March 1, 2024 09:26
andrew-farries added a commit that referenced this pull request Mar 6, 2024
…ions (#308)

#297 added the ability to specify the `ON DELETE` behavior of a foreign
key when altering an existing column.

Make it so that the `ON DELETE` property of the the FK is respected for
FKs on new columns too (ie those created by add column and create table
operations).

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

Successfully merging this pull request may close these issues.

Support setting up ON DELETE on foreign keys
2 participants