from typing import Optional from sqlalchemy import ForeignKey from sqlalchemy.orm import Mapped, mapped_column, relationship from models.base import BaseModel from models.mixins import IdMixin, SoftDeleteMixin from modules.clients.models import Client from modules.fulfillment_base.models import ( ProductBarcode, BarcodeTemplate, ProductBarcodeImage, ) class Product(BaseModel, IdMixin, SoftDeleteMixin): __tablename__ = "fulfillment_base_products" name: Mapped[str] = mapped_column() article: Mapped[str] = mapped_column(index=True) factory_article: Mapped[str] = mapped_column( index=True, default="", server_default="" ) brand: Mapped[str] = mapped_column(default="", comment="Бренд") color: Mapped[str] = mapped_column(default="", comment="Цвет") composition: Mapped[str] = mapped_column(default="", comment="Состав") size: Mapped[str] = mapped_column(default="", comment="Размер") additional_info: Mapped[str] = mapped_column( default="", comment="Дополнительная информация" ) client_id: Mapped[int] = mapped_column(ForeignKey("clients.id")) client: Mapped["Client"] = relationship(back_populates="products") images: Mapped[list["ProductImage"]] = relationship( "ProductImage", back_populates="product", lazy="selectin", cascade="all, delete-orphan", ) barcodes: Mapped[list["ProductBarcode"]] = relationship( back_populates="product", cascade="all, delete-orphan", ) barcode_template_id: Mapped[Optional[int]] = mapped_column( ForeignKey("barcode_templates.id") ) barcode_template: Mapped["BarcodeTemplate"] = relationship(lazy="joined") barcode_image: Mapped["ProductBarcodeImage"] = relationship( back_populates="product", lazy="joined", uselist=False, ) class ProductImage(BaseModel, IdMixin): __tablename__ = "fulfillment_base_product_images" product_id: Mapped[int] = mapped_column(ForeignKey("fulfillment_base_products.id")) product: Mapped["Product"] = relationship(back_populates="images") image_url: Mapped[str] = mapped_column(nullable=False)