Add quantity column to GuestGifts table and update references

Introduced a `quantity` field to track the number of reserved gifts in the many-to-many GuestGifts table. Updated all related CRUD operations, models, and imports to reflect the added column. Replaced `guest_gifts` with `GuestGifts` for consistency in naming conventions.
This commit is contained in:
2025-03-19 09:56:30 +01:00
parent 2c73ee4d7e
commit 79f08a1208
4 changed files with 12 additions and 9 deletions

View File

@@ -317,6 +317,7 @@ 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.Column('quantity', sa.Integer(), nullable=False, server_default='1'),
sa.ForeignKeyConstraint(['gift_id'], ['gift_items.id'], ondelete="CASCADE"),
sa.ForeignKeyConstraint(['guest_id'], ['guests.id'], ondelete="CASCADE"),
sa.PrimaryKeyConstraint('guest_id', 'gift_id')

View File

@@ -94,13 +94,14 @@ class CRUDGiftItem(CRUDBase[GiftItem, GiftItemCreate, GiftItemUpdate]):
gift: GiftItem = self.get(db, gift_id)
if gift and gift.quantity_received < gift.quantity_requested:
# Add to the association table using the SQLAlchemy Core Table directly
from app.models.guest import guest_gifts
from app.models.guest import GuestGifts
stmt = guest_gifts.insert().values(
stmt = GuestGifts.insert().values(
gift_id=gift_id,
guest_id=guest_id,
reserved_at=datetime.now(timezone.utc),
notes=notes
notes=notes,
quantity=quantity
)
db.execute(stmt)
@@ -336,12 +337,12 @@ class CRUDGiftPurchase(CRUDBase[GiftPurchase, GiftPurchaseCreate, GiftPurchaseUp
for gift in guest.gifts:
# Access the association data through the relationship metadata
from sqlalchemy import select
from app.models.guest import guest_gifts
from app.models.guest import GuestGifts
# Get the reservation data
stmt = select(guest_gifts).where(
(guest_gifts.c.guest_id == guest_id) &
(guest_gifts.c.gift_id == gift.id)
stmt = select(GuestGifts).where(
(GuestGifts.c.guest_id == guest_id) &
(GuestGifts.c.gift_id == gift.id)
)
reservation = db.execute(stmt).first()

View File

@@ -16,7 +16,7 @@ from .event_theme import EventTheme
from .event_media import EventMedia, MediaType, MediaPurpose
# Import guest and RSVP models
from .guest import Guest, GuestStatus, guest_gifts
from .guest import Guest, GuestStatus, GuestGifts
from .rsvp import RSVP, RSVPStatus
# Import gift-related models

View File

@@ -82,11 +82,12 @@ class Guest(Base, UUIDMixin, TimestampMixin):
# Association table for guest gifts (many-to-many relationship)
guest_gifts = Table(
GuestGifts = Table(
'guest_gifts',
Base.metadata,
Column('guest_id', UUID(as_uuid=True), ForeignKey('guests.id'), primary_key=True),
Column('gift_id', UUID(as_uuid=True), ForeignKey('gift_items.id'), primary_key=True),
Column('reserved_at', DateTime(timezone=True), default=lambda: datetime.now(timezone.utc)),
Column('notes', String),
Column('quantity', Integer, default=1),
)