fix(models): cascade delete inventory rows when product is deleted

SQLAlchemy was nulling out product_id on ProductInventory rows instead
of deleting them. Added cascade="all, delete-orphan" to the ORM
relationship and ondelete="CASCADE" to the FK field.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Piotr Oleszczyk 2026-02-28 22:48:00 +01:00
parent ef8334b93c
commit 2691708304

View file

@ -202,7 +202,10 @@ class Product(ProductBase, table=True):
), ),
) )
inventory: list["ProductInventory"] = Relationship(back_populates="product") inventory: list["ProductInventory"] = Relationship(
back_populates="product",
sa_relationship_kwargs={"cascade": "all, delete-orphan"},
)
@field_validator("product_effect_profile", mode="before") @field_validator("product_effect_profile", mode="before")
@classmethod @classmethod
@ -378,7 +381,7 @@ class ProductInventory(SQLModel, table=True):
__domains__: ClassVar[frozenset[Domain]] = frozenset({Domain.SKINCARE}) __domains__: ClassVar[frozenset[Domain]] = frozenset({Domain.SKINCARE})
id: UUID = Field(default_factory=uuid4, primary_key=True) id: UUID = Field(default_factory=uuid4, primary_key=True)
product_id: UUID = Field(foreign_key="products.id", index=True) product_id: UUID = Field(foreign_key="products.id", index=True, ondelete="CASCADE")
is_opened: bool = Field(default=False) is_opened: bool = Field(default=False)
opened_at: date | None = Field(default=None) opened_at: date | None = Field(default=None)