48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
import uuid
|
|
from datetime import datetime
|
|
from typing import TYPE_CHECKING
|
|
|
|
from sqlalchemy import (
|
|
CheckConstraint,
|
|
DateTime,
|
|
ForeignKey,
|
|
Integer,
|
|
String,
|
|
UniqueConstraint,
|
|
Uuid,
|
|
func,
|
|
)
|
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
|
|
from app.database import Base
|
|
|
|
if TYPE_CHECKING:
|
|
from app.models.category import Category
|
|
|
|
|
|
class Budget(Base):
|
|
__tablename__ = "budgets"
|
|
__table_args__ = (
|
|
UniqueConstraint("user_id", "category_id", "month", name="uq_budgets_user_category_month"),
|
|
CheckConstraint("limit_cents > 0", name="ck_budgets_limit_positive"),
|
|
)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, default=uuid.uuid4)
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
Uuid, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True
|
|
)
|
|
category_id: Mapped[uuid.UUID] = mapped_column(
|
|
Uuid, ForeignKey("categories.id"), nullable=False
|
|
)
|
|
# Format YYYY-MM
|
|
month: Mapped[str] = mapped_column(String(7), nullable=False)
|
|
limit_cents: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=False), nullable=False, server_default=func.now()
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=False), nullable=False, server_default=func.now()
|
|
)
|
|
|
|
category: Mapped["Category"] = relationship("Category", lazy="raise")
|