Add cascading delete to Gift and Guest relationships
Updated the `ondelete="CASCADE"` behavior for foreign key constraints in Alembic migrations and added cascading rules to related models in SQLAlchemy. These changes ensure proper cleanup of dependent records when a parent record is deleted.
This commit is contained in:
@@ -317,8 +317,8 @@ def upgrade() -> None:
|
||||
sa.Column('gift_id', sa.UUID(), nullable=False),
|
||||
sa.Column('reserved_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column('notes', sa.String(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['gift_id'], ['gift_items.id'], ),
|
||||
sa.ForeignKeyConstraint(['guest_id'], ['guests.id'], ),
|
||||
sa.ForeignKeyConstraint(['gift_id'], ['gift_items.id'], ondelete="CASCADE"),
|
||||
sa.ForeignKeyConstraint(['guest_id'], ['guests.id'], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint('guest_id', 'gift_id')
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
@@ -68,10 +68,10 @@ class GiftItem(Base, UUIDMixin, TimestampMixin):
|
||||
category = relationship("GiftCategory", back_populates="gifts")
|
||||
reserved_by = relationship("Guest",
|
||||
secondary="guest_gifts",
|
||||
back_populates="gifts")
|
||||
back_populates="gifts", cascade='all')
|
||||
purchase_history = relationship("GiftPurchase",
|
||||
back_populates="gift_item",
|
||||
order_by="desc(GiftPurchase.purchased_at)")
|
||||
order_by="desc(GiftPurchase.purchased_at)", cascade='all, delete-orphan')
|
||||
|
||||
def __repr__(self):
|
||||
return f"<GiftItem {self.name} ({self.status.value})>"
|
||||
|
||||
Reference in New Issue
Block a user