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

Support setting up ON DELETE on foreign keys #221

Closed
AA-morganh opened this issue Jan 6, 2024 · 0 comments · Fixed by #297
Closed

Support setting up ON DELETE on foreign keys #221

AA-morganh opened this issue Jan 6, 2024 · 0 comments · Fixed by #297
Assignees

Comments

@AA-morganh
Copy link

Currently there's not a way to set up ON DELETE options when setting up reference columns (foreign keys).

Specifically, ON DELETE CASCADE can be an extremely powerful tool for powering scenarios such as right to be forgotten requests. While setting this up can be accomplished through raw sql migrations, it would be ideal to have this as a setting on the reference itself.

@andrew-farries andrew-farries self-assigned this Feb 28, 2024
andrew-farries added a commit that referenced this issue Mar 1, 2024
)

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

An example migration that uses the behaviour is:

```json
{
  "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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants