diff --git a/dibbler/models/Purchase.py b/dibbler/models/Purchase.py index 42c4c52..be6873d 100644 --- a/dibbler/models/Purchase.py +++ b/dibbler/models/Purchase.py @@ -1,8 +1,8 @@ from __future__ import annotations -from typing import TYPE_CHECKING -from datetime import datetime import math +from datetime import datetime +from typing import TYPE_CHECKING from sqlalchemy import ( DateTime, @@ -29,7 +29,8 @@ class Purchase(Base): price: Mapped[int] = mapped_column(Integer) transactions: Mapped[set[Transaction]] = relationship( - back_populates="purchase", order_by="Transaction.user_name" + back_populates="purchase", + order_by=Transaction.user_name, ) entries: Mapped[set[PurchaseEntry]] = relationship(back_populates="purchase") diff --git a/dibbler/models/PurchaseEntry.py b/dibbler/models/PurchaseEntry.py index 8484b32..4989473 100644 --- a/dibbler/models/PurchaseEntry.py +++ b/dibbler/models/PurchaseEntry.py @@ -27,8 +27,8 @@ class PurchaseEntry(Base): product_id: Mapped[int] = mapped_column(ForeignKey("products.product_id")) purchase_id: Mapped[int] = mapped_column(ForeignKey("purchases.id")) - product: Mapped[Product] = relationship(lazy="joined") - purchase: Mapped[Purchase] = relationship(lazy="joined") + product: Mapped[Product] = relationship(back_populates='purchases', lazy="joined") + purchase: Mapped[Purchase] = relationship(back_populates='entries', lazy="joined") def __init__(self, purchase, product, amount): self.product = product diff --git a/dibbler/models/Transaction.py b/dibbler/models/Transaction.py index 6c509ef..8a3626b 100644 --- a/dibbler/models/Transaction.py +++ b/dibbler/models/Transaction.py @@ -36,7 +36,7 @@ class Transaction(Base): purchase_id: Mapped[int | None] = mapped_column(ForeignKey("purchases.id")) user: Mapped[User] = relationship(lazy="joined") - purchase: Mapped[Purchase] = relationship(lazy="joined") + purchase: Mapped[Purchase] = relationship(back_populates='transactions', lazy="joined") def __init__( self, diff --git a/dibbler/models/User.py b/dibbler/models/User.py index 1fd91c1..2409d8d 100644 --- a/dibbler/models/User.py +++ b/dibbler/models/User.py @@ -1,4 +1,5 @@ from __future__ import annotations + from typing import TYPE_CHECKING from sqlalchemy import ( @@ -14,19 +15,22 @@ from sqlalchemy.orm import ( from .Base import Base if TYPE_CHECKING: - from .UserProducts import UserProducts from .Transaction import Transaction + from .UserProducts import UserProducts class User(Base): __tablename__ = "users" name: Mapped[str] = mapped_column(String(10), primary_key=True) - credit: Mapped[str] = mapped_column(Integer) + credit: Mapped[int] = mapped_column(Integer) card: Mapped[str | None] = mapped_column(String(20)) rfid: Mapped[str | None] = mapped_column(String(20)) products: Mapped[list[UserProducts]] = relationship(back_populates="user") - transactions: Mapped[list[Transaction]] = relationship(back_populates="user") + transactions: Mapped[list[Transaction]] = relationship( + back_populates="user", + order_by="Transaction.time", + ) name_re = r"[a-z]+" card_re = r"(([Nn][Tt][Nn][Uu])?[0-9]+)?" diff --git a/dibbler/models/UserProducts.py b/dibbler/models/UserProducts.py index 17a8f13..a3448d9 100644 --- a/dibbler/models/UserProducts.py +++ b/dibbler/models/UserProducts.py @@ -27,5 +27,5 @@ class UserProducts(Base): count: Mapped[int] = mapped_column(Integer) sign: Mapped[int] = mapped_column(Integer) - user: Mapped[User] = relationship() - product: Mapped[Product] = relationship() + user: Mapped[User] = relationship(back_populates="products") + product: Mapped[Product] = relationship(back_populates="users")