from typing import TYPE_CHECKING from sqlalchemy import ForeignKey, ForeignKeyConstraint from sqlalchemy.orm import Mapped, mapped_column, relationship from models.base import BaseModel from models.mixins import PriceMixin if TYPE_CHECKING: from models import Deal, Service, Product class DealProduct(BaseModel): __tablename__ = "fulfillment_base_deal_products" deal_id: Mapped[int] = mapped_column(ForeignKey("deals.id"), primary_key=True) product_id: Mapped[int] = mapped_column( ForeignKey("fulfillment_base_products.id"), primary_key=True ) quantity: Mapped[int] = mapped_column(default=1) comment: Mapped[str] = mapped_column(comment="Комментарий к товару") deal: Mapped["Deal"] = relationship(backref="deal_products") product: Mapped["Product"] = relationship() product_services: Mapped[list["DealProductService"]] = relationship( back_populates="deal_product", primaryjoin="and_(DealProduct.deal_id==DealProductService.deal_id, DealProduct.product_id==DealProductService.product_id)", ) class DealProductService(BaseModel, PriceMixin): __tablename__ = "fulfillment_base_deal_products_services" deal_id: Mapped[int] = mapped_column(primary_key=True) product_id: Mapped[int] = mapped_column(primary_key=True) service_id: Mapped[int] = mapped_column( ForeignKey("fulfillment_base_services.id"), primary_key=True ) is_fixed_price: Mapped[bool] = mapped_column( default=False, server_default="0", comment="Фиксированная цена" ) deal_product: Mapped["DealProduct"] = relationship( back_populates="product_services", primaryjoin="and_(DealProductService.deal_id==DealProduct.deal_id, DealProductService.product_id==DealProduct.product_id)", ) service: Mapped["Service"] = relationship() __table_args__ = ( ForeignKeyConstraint( ["deal_id", "product_id"], [ "fulfillment_base_deal_products.deal_id", "fulfillment_base_deal_products.product_id", ], ), )