from typing import TYPE_CHECKING from sqlalchemy import ForeignKey, UniqueConstraint from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.orm import Mapped, mapped_column, relationship from models.base import BaseModel from models.mixins import IdMixin, SoftDeleteMixin if TYPE_CHECKING: from models import Attribute class AttributeSelect(BaseModel, IdMixin, SoftDeleteMixin): __tablename__ = "attribute_selects" label: Mapped[str] = mapped_column() is_built_in: Mapped[bool] = mapped_column( default=False, comment="Если встроенный select, то запрещено редактировать пользователю", ) options: Mapped[list["AttributeOption"]] = relationship( back_populates="select", lazy="noload", ) attributes: Mapped[list["Attribute"]] = relationship( back_populates="select", lazy="noload", ) class AttributeOption(BaseModel, IdMixin, SoftDeleteMixin): __tablename__ = "attribute_options" value: Mapped[dict[str, any]] = mapped_column(JSONB) label: Mapped[str] = mapped_column() select_id: Mapped[int] = mapped_column(ForeignKey("attribute_selects.id")) select: Mapped[AttributeSelect] = relationship( back_populates="options", lazy="noload", ) __table_args__ = (UniqueConstraint("value", "select_id", name="_value_select_uc"),)