fix(models): add cascade delete-orphan to parent-child relationships

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 <noreply@anthropic.com>
This commit is contained in:
Piotr Oleszczyk 2026-03-01 00:59:10 +01:00
parent 832676bcfa
commit f72d5ba1b7
2 changed files with 8 additions and 2 deletions

View file

@ -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): class MedicationUsage(SQLModel, table=True):

View file

@ -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): class GroomingSchedule(SQLModel, table=True):