59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
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 Deal, Product
|
|
|
|
|
|
class Pallet(BaseModel):
|
|
__tablename__ = 'pallets'
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
|
|
|
deal_id: Mapped[int] = mapped_column(ForeignKey('deals.id'))
|
|
deal: Mapped['Deal'] = 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')
|
|
|
|
deal_id: Mapped[Optional[int]] = mapped_column(ForeignKey('deals.id'))
|
|
deal: Mapped['Deal'] = relationship(back_populates='boxes')
|