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.
 
 
 
 
 

85 lines
3.0 KiB

from __future__ import annotations
from datetime import UTC, datetime
from decimal import Decimal
from typing import Any
from sqlalchemy import DateTime, ForeignKey, Index, Numeric, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.models.base import BaseModel, ID_TYPE, TimestampMixin
class GiftRecord(BaseModel, TimestampMixin):
__tablename__ = "gift_records"
__table_args__ = (
Index("idx_gift_records_household_id", "household_id"),
Index("idx_gift_records_record_time", "record_time"),
Index("idx_gift_records_household_record_time", "household_id", "record_time"),
Index("idx_gift_records_record_type", "record_type"),
Index("idx_gift_records_deleted_at", "deleted_at"),
)
id: Mapped[int] = mapped_column(ID_TYPE, primary_key=True)
household_id: Mapped[int] = mapped_column(
ID_TYPE,
ForeignKey("households.id", ondelete="RESTRICT"),
nullable=False,
)
record_type: Mapped[str] = mapped_column(
String(32),
nullable=False,
default="cash_gift",
)
amount: Mapped[Decimal | None] = mapped_column(Numeric(12, 2), nullable=True)
gift_name: Mapped[str | None] = mapped_column(String(128), nullable=True)
estimated_value: Mapped[Decimal | None] = mapped_column(Numeric(12, 2), nullable=True)
method_option_id: Mapped[int | None] = mapped_column(
ID_TYPE,
ForeignKey("option_items.id", ondelete="RESTRICT"),
nullable=True,
)
scene_option_id: Mapped[int | None] = mapped_column(
ID_TYPE,
ForeignKey("option_items.id", ondelete="RESTRICT"),
nullable=True,
)
record_time: Mapped[datetime] = mapped_column(
DateTime,
nullable=False,
default=lambda: datetime.now(UTC).replace(tzinfo=None),
)
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,
)
deleted_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
note: Mapped[str | None] = mapped_column(Text, nullable=True)
household: Mapped[Any] = relationship("Household", back_populates="gift_records")
method_option: Mapped[Any] = relationship(
"OptionItem",
foreign_keys=[method_option_id],
back_populates="gift_records_as_method",
)
scene_option: Mapped[Any] = relationship(
"OptionItem",
foreign_keys=[scene_option_id],
back_populates="gift_records_as_scene",
)
created_by_account: Mapped[Any] = relationship(
"Account",
foreign_keys=[created_by],
back_populates="created_gift_records",
)
updated_by_account: Mapped[Any] = relationship(
"Account",
foreign_keys=[updated_by],
back_populates="updated_gift_records",
)