Files
aurum/backend/app/models/transaction.py
ZProger 94782ee073 feat: add free-form tags on transactions
Third of the four ideas picked from the Maybe/Sure feature audit. Tags are
orthogonal to categories (category = what kind of spend, tag = which
event/project) — many-to-many, created inline while typing in the
transaction form, filterable from the Transactions page.

Also adds tests/test_backup.py, the first test coverage the backup
export/import path has ever had, and fixes a MissingGreenlet bug it
surfaced in restore_backup's tag-wiring.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-25 02:38:04 +03:00

38 lines
1.9 KiB
Python

"""A single money movement: income, expense, or a transfer between accounts."""
from datetime import date as date_
from sqlalchemy import Date, Enum, ForeignKey, Numeric, String, Text
from sqlalchemy.orm import Mapped, mapped_column, relationship
from app.db.base import Base
from app.models.enums import TransactionType
from app.models.mixins import TimestampMixin
from app.models.tag import transaction_tags
class Transaction(Base, TimestampMixin):
__tablename__ = "transactions"
id: Mapped[int] = mapped_column(primary_key=True)
account_id: Mapped[int] = mapped_column(ForeignKey("accounts.id", ondelete="CASCADE"), nullable=False)
category_id: Mapped[int | None] = mapped_column(ForeignKey("categories.id", ondelete="SET NULL"), nullable=True)
# Destination account for TRANSFER-type rows only.
transfer_account_id: Mapped[int | None] = mapped_column(
ForeignKey("accounts.id", ondelete="SET NULL"), nullable=True
)
type: Mapped[TransactionType] = mapped_column(
Enum(TransactionType, name="transaction_type", native_enum=False, length=10), nullable=False
)
# Always stored positive; `type` carries the sign/direction.
amount: Mapped[Numeric] = mapped_column(Numeric(14, 2), nullable=False)
description: Mapped[str] = mapped_column(String(255), nullable=False)
merchant: Mapped[str | None] = mapped_column(String(150), nullable=True)
notes: Mapped[str | None] = mapped_column(Text, nullable=True)
date: Mapped[date_] = mapped_column(Date, nullable=False)
account: Mapped["Account"] = relationship(back_populates="transactions", foreign_keys=[account_id])
transfer_account: Mapped["Account | None"] = relationship(foreign_keys=[transfer_account_id])
category: Mapped["Category | None"] = relationship(back_populates="transactions")
tags: Mapped[list["Tag"]] = relationship(secondary=transaction_tags, back_populates="transactions")