Skip to content

fix(db): add ON DELETE CASCADE to study FK constraints#820

Merged
ibrahimozkn merged 1 commit into
devfrom
fix/study-cascade-delete
May 16, 2026
Merged

fix(db): add ON DELETE CASCADE to study FK constraints#820
ibrahimozkn merged 1 commit into
devfrom
fix/study-cascade-delete

Conversation

@ibrahimozkn
Copy link
Copy Markdown
Contributor

Problem

Deleting a study from the backend threw a PostgrestException (23503 FK violation) when dependent records existed in study_invite, study_subject, repo, or study_fitbit_credentials. These four FK constraints referenced study(id) without ON DELETE CASCADE, blocking the delete entirely.

Changes

  • Added migration database/migration/20260425_study_cascade_delete.sql — drops and re-adds all four FK constraints with ON DELETE CASCADE
  • Updated database/studyu-schema.sql to reflect the new constraint definitions

Testing

  • Run migration against Supabase instance and verify it applies cleanly
  • Delete a study that has existing invites, subjects, repo entry, and Fitbit credentials — confirm it succeeds without FK error
  • Verify dependent records are removed from all four tables after deletion
  • No regressions in study creation or subject enrollment flows

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.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 with ON DELETE CASCADE.
  • Updated database/studyu-schema.sql to 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.

Comment thread database/migration/20260425_study_cascade_delete.sql
Copy link
Copy Markdown
Collaborator

@mohiuddinshahrukh mohiuddinshahrukh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Everything looks good here, tested it locally and works fine 👍🏼

@ibrahimozkn ibrahimozkn enabled auto-merge May 16, 2026 11:50
@ibrahimozkn ibrahimozkn merged commit d80fc81 into dev May 16, 2026
11 checks passed
@ibrahimozkn ibrahimozkn deleted the fix/study-cascade-delete branch May 16, 2026 11:50
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.

3 participants