You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.6 KiB
100 lines
3.6 KiB
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from sqlalchemy import ForeignKey, Index, String, UniqueConstraint
|
|
from sqlalchemy.types import JSON
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.models.base import BaseModel, ID_TYPE, TimestampMixin
|
|
|
|
|
|
class OptionItem(BaseModel, TimestampMixin):
|
|
__tablename__ = "option_items"
|
|
__table_args__ = (
|
|
UniqueConstraint("option_group", "option_code", name="uk_option_items_group_code"),
|
|
UniqueConstraint(
|
|
"option_group",
|
|
"parent_id",
|
|
"option_label",
|
|
name="uk_option_items_group_parent_label",
|
|
),
|
|
Index(
|
|
"idx_option_items_group_enabled_sort",
|
|
"option_group",
|
|
"is_enabled",
|
|
"sort_order",
|
|
),
|
|
Index("idx_option_items_parent_id", "parent_id"),
|
|
)
|
|
|
|
id: Mapped[int] = mapped_column(ID_TYPE, primary_key=True)
|
|
option_group: Mapped[str] = mapped_column(String(32), nullable=False)
|
|
option_code: Mapped[str] = mapped_column(String(64), nullable=False)
|
|
option_label: Mapped[str] = mapped_column(String(64), nullable=False)
|
|
parent_id: Mapped[int | None] = mapped_column(
|
|
ID_TYPE,
|
|
ForeignKey("option_items.id", ondelete="RESTRICT"),
|
|
nullable=True,
|
|
)
|
|
sort_order: Mapped[int] = mapped_column(nullable=False, default=0)
|
|
is_enabled: Mapped[bool] = mapped_column(nullable=False, default=True)
|
|
is_system: Mapped[bool] = mapped_column(nullable=False, default=False)
|
|
extra_json: Mapped[dict[str, object] | list[object] | None] = mapped_column(JSON, nullable=True)
|
|
created_by: Mapped[int | None] = mapped_column(
|
|
ID_TYPE,
|
|
ForeignKey("accounts.id", ondelete="SET NULL"),
|
|
nullable=True,
|
|
)
|
|
updated_by: Mapped[int | None] = mapped_column(
|
|
ID_TYPE,
|
|
ForeignKey("accounts.id", ondelete="SET NULL"),
|
|
nullable=True,
|
|
)
|
|
|
|
parent: Mapped[Any] = relationship(
|
|
"OptionItem",
|
|
remote_side="OptionItem.id",
|
|
back_populates="children",
|
|
)
|
|
children: Mapped[list[Any]] = relationship("OptionItem", back_populates="parent")
|
|
created_by_account: Mapped[Any] = relationship(
|
|
"Account",
|
|
foreign_keys=[created_by],
|
|
back_populates="created_option_items",
|
|
)
|
|
updated_by_account: Mapped[Any] = relationship(
|
|
"Account",
|
|
foreign_keys=[updated_by],
|
|
back_populates="updated_option_items",
|
|
)
|
|
households_as_relation_category: Mapped[list[Any]] = relationship(
|
|
"Household",
|
|
foreign_keys="Household.relation_category_option_id",
|
|
back_populates="relation_category_option",
|
|
)
|
|
households_as_relation_detail: Mapped[list[Any]] = relationship(
|
|
"Household",
|
|
foreign_keys="Household.relation_detail_option_id",
|
|
back_populates="relation_detail_option",
|
|
)
|
|
households_as_gift_method: Mapped[list[Any]] = relationship(
|
|
"Household",
|
|
foreign_keys="Household.gift_method_option_id",
|
|
back_populates="gift_method_option",
|
|
)
|
|
households_as_gift_scene: Mapped[list[Any]] = relationship(
|
|
"Household",
|
|
foreign_keys="Household.gift_scene_option_id",
|
|
back_populates="gift_scene_option",
|
|
)
|
|
gift_records_as_method: Mapped[list[Any]] = relationship(
|
|
"GiftRecord",
|
|
foreign_keys="GiftRecord.method_option_id",
|
|
back_populates="method_option",
|
|
)
|
|
gift_records_as_scene: Mapped[list[Any]] = relationship(
|
|
"GiftRecord",
|
|
foreign_keys="GiftRecord.scene_option_id",
|
|
back_populates="scene_option",
|
|
)
|
|
|