Is there an existing issue for this?
What happened?
📌 Issue Overview
supabase/schemas/schema.sql has three related database integrity problems that together make the schema non-executable from scratch, allow duplicate medical records, and leave orphaned rows on user deletion.
🔍 Steps to Reproduce
Issue 1 — Missing therapy_type table:
- Run
schema.sql against a fresh Postgres/Supabase instance.
- Observe:
ERROR: relation "therapy_type" does not exist on therapy_goal creation.
Issue 2 — Duplicate assessment submissions:
- Open the assessment submission screen on a slow connection.
- Tap submit twice before the response returns.
- Two rows now exist in
assessment_results for the same patient_id + assessment_id.
Issue 3 — No ON DELETE behavior:
- Insert a patient with linked sessions, activity logs, and assessment results.
- Attempt to delete the patient record.
- Observe either a FK violation error or orphaned rows left in child tables.
🎯 Expected Behavior
- Schema should be executable from scratch without errors.
- Each patient should have at most one result per assessment.
- Deleting a patient should cascade to their owned data; deleting a therapist should set
therapist_id to NULL in related records.
🚨 Actual Behavior
therapy_goal references therapy_type(id) which does not exist in the schema.
- Duplicate assessment results can be silently inserted into medical records.
- No
ON DELETE rules defined anywhere — orphaned data or blocked deletions.
📷 Screenshot
N/A
💡 Suggested Improvements
- Add
CREATE TABLE therapy_type before therapy_goal in schema.sql.
- Add
CONSTRAINT uq_patient_assessment UNIQUE (patient_id, assessment_id) to assessment_results.
- Add
ON DELETE CASCADE or ON DELETE SET NULL to all foreign keys based on relationship semantics.
- Change
.insert() to .upsert() with onConflict: 'patient_id,assessment_id' in the Edge Function.
Record
Is there an existing issue for this?
What happened?
📌 Issue Overview
supabase/schemas/schema.sqlhas three related database integrity problems that together make the schema non-executable from scratch, allow duplicate medical records, and leave orphaned rows on user deletion.🔍 Steps to Reproduce
Issue 1 — Missing therapy_type table:
schema.sqlagainst a fresh Postgres/Supabase instance.ERROR: relation "therapy_type" does not existontherapy_goalcreation.Issue 2 — Duplicate assessment submissions:
assessment_resultsfor the samepatient_id+assessment_id.Issue 3 — No ON DELETE behavior:
🎯 Expected Behavior
therapist_idto NULL in related records.🚨 Actual Behavior
therapy_goalreferencestherapy_type(id)which does not exist in the schema.ON DELETErules defined anywhere — orphaned data or blocked deletions.📷 Screenshot
N/A
💡 Suggested Improvements
CREATE TABLE therapy_typebeforetherapy_goalinschema.sql.CONSTRAINT uq_patient_assessment UNIQUE (patient_id, assessment_id)toassessment_results.ON DELETE CASCADEorON DELETE SET NULLto all foreign keys based on relationship semantics..insert()to.upsert()withonConflict: 'patient_id,assessment_id'in the Edge Function.Record