from typing import TYPE_CHECKING, Optional from sqlalchemy import ForeignKey from sqlalchemy.orm import Mapped, mapped_column, relationship from models import BaseModel if TYPE_CHECKING: from models import Card, Product, Client class Pallet(BaseModel): __tablename__ = 'pallets' id: Mapped[int] = mapped_column(primary_key=True) card_id: Mapped[int] = mapped_column(ForeignKey('cards.id')) card: Mapped['Card'] = relationship(back_populates='pallets') boxes: Mapped[list['Box']] = relationship( back_populates='pallet', uselist=True, lazy='joined', cascade='all, delete-orphan', ) shipping_products: Mapped[list['ShippingProduct']] = relationship( back_populates='pallet', uselist=True, lazy='joined', cascade='all, delete-orphan', ) class ShippingProduct(BaseModel): __tablename__ = 'shipping_products' id: Mapped[int] = mapped_column(primary_key=True) quantity: Mapped[int] = mapped_column() product_id: Mapped[int] = mapped_column(ForeignKey('products.id')) product: Mapped['Product'] = relationship(lazy='joined') pallet_id: Mapped[int] = mapped_column(ForeignKey('pallets.id')) pallet: Mapped['Pallet'] = relationship(lazy='joined') class Box(BaseModel): __tablename__ = 'boxes' id: Mapped[int] = mapped_column(primary_key=True) quantity: Mapped[int] = mapped_column(default=0) product_id: Mapped[Optional[int]] = mapped_column(ForeignKey('products.id'), nullable=True) product: Mapped['Product'] = relationship(lazy='joined') pallet_id: Mapped[Optional[int]] = mapped_column(ForeignKey('pallets.id')) pallet: Mapped[Pallet] = relationship(back_populates='boxes') card_id: Mapped[Optional[int]] = mapped_column(ForeignKey('cards.id')) card: Mapped['Card'] = relationship(back_populates='boxes')