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:
parent
ef8334b93c
commit
2691708304
1 changed files with 5 additions and 2 deletions
|
|
@ -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")
|
||||
@classmethod
|
||||
|
|
@ -378,7 +381,7 @@ class ProductInventory(SQLModel, table=True):
|
|||
__domains__: ClassVar[frozenset[Domain]] = frozenset({Domain.SKINCARE})
|
||||
|
||||
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)
|
||||
opened_at: date | None = Field(default=None)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue