From f72d5ba1b70e294607bc310f277f0dc7c6709cc5 Mon Sep 17 00:00:00 2001 From: Piotr Oleszczyk Date: Sun, 1 Mar 2026 00:59:10 +0100 Subject: [PATCH] fix(models): add cascade delete-orphan to parent-child relationships MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Without cascade, SQLAlchemy tried to NULL-out foreign keys on child rows before deleting the parent, hitting NOT NULL constraints in PostgreSQL. - Routine.steps: cascade="all, delete-orphan" (routine_steps.routine_id) - MedicationEntry.usage_history: cascade="all, delete-orphan" (medication_usages.medication_record_id) Product.inventory already had cascade set correctly. No DB migration needed — ORM-level only. Co-Authored-By: Claude Sonnet 4.6 --- backend/innercontext/models/health.py | 5 ++++- backend/innercontext/models/routine.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/backend/innercontext/models/health.py b/backend/innercontext/models/health.py index 2aac33d..419fb11 100644 --- a/backend/innercontext/models/health.py +++ b/backend/innercontext/models/health.py @@ -36,7 +36,10 @@ class MedicationEntry(SQLModel, table=True): ), ) - usage_history: list["MedicationUsage"] = Relationship(back_populates="medication") + usage_history: list["MedicationUsage"] = Relationship( + back_populates="medication", + sa_relationship_kwargs={"cascade": "all, delete-orphan"}, + ) class MedicationUsage(SQLModel, table=True): diff --git a/backend/innercontext/models/routine.py b/backend/innercontext/models/routine.py index d44a20a..cdd5796 100644 --- a/backend/innercontext/models/routine.py +++ b/backend/innercontext/models/routine.py @@ -38,7 +38,10 @@ class Routine(SQLModel, table=True): ), ) - steps: List["RoutineStep"] = Relationship(back_populates="routine") + steps: List["RoutineStep"] = Relationship( + back_populates="routine", + sa_relationship_kwargs={"cascade": "all, delete-orphan"}, + ) class GroomingSchedule(SQLModel, table=True):