Add cascading delete to Gift and Guest relationships
All checks were successful
Build and Push Docker Images / changes (push) Successful in 5s
Build and Push Docker Images / build-backend (push) Successful in 49s
Build and Push Docker Images / build-frontend (push) Has been skipped

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:
2025-03-19 08:16:38 +01:00
parent 7d8eacad7a
commit 1441843a55
2 changed files with 4 additions and 4 deletions

View File

@@ -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 ###

View File

@@ -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})>"