fix(db): add ON DELETE CASCADE to study FK constraints#820
Merged
Conversation
Study deletion failed with FK violation (23503) when dependent records existed in study_invite, study_subject, repo, or study_fitbit_credentials. Drop and re-add all four constraints with ON DELETE CASCADE so cascading cleanup is handled at the DB level without app-side workarounds.
Contributor
There was a problem hiding this comment.
Pull request overview
Updates database foreign key constraints so deleting a study cascades to dependent rows, preventing FK-violation errors during study deletion.
Changes:
- Added a new SQL migration to drop/recreate four
study(id)foreign keys withON DELETE CASCADE. - Updated
database/studyu-schema.sqlto reflect the new cascade delete behavior.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
database/studyu-schema.sql |
Updates the schema snapshot so the four study_id FKs now cascade on study deletion. |
database/migration/20260425_study_cascade_delete.sql |
Adds a migration that drops and re-adds the four FK constraints with ON DELETE CASCADE. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
mohiuddinshahrukh
approved these changes
Apr 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Deleting a study from the backend threw a PostgrestException (23503 FK violation) when dependent records existed in
study_invite,study_subject,repo, orstudy_fitbit_credentials. These four FK constraints referencedstudy(id)withoutON DELETE CASCADE, blocking the delete entirely.Changes
database/migration/20260425_study_cascade_delete.sql— drops and re-adds all four FK constraints withON DELETE CASCADEdatabase/studyu-schema.sqlto reflect the new constraint definitionsTesting