models: a bit of back population
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from typing import TYPE_CHECKING
|
|
||||||
|
|
||||||
from datetime import datetime
|
|
||||||
import math
|
import math
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from sqlalchemy import (
|
from sqlalchemy import (
|
||||||
DateTime,
|
DateTime,
|
||||||
@@ -29,7 +29,8 @@ class Purchase(Base):
|
|||||||
price: Mapped[int] = mapped_column(Integer)
|
price: Mapped[int] = mapped_column(Integer)
|
||||||
|
|
||||||
transactions: Mapped[set[Transaction]] = relationship(
|
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")
|
entries: Mapped[set[PurchaseEntry]] = relationship(back_populates="purchase")
|
||||||
|
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ class PurchaseEntry(Base):
|
|||||||
product_id: Mapped[int] = mapped_column(ForeignKey("products.product_id"))
|
product_id: Mapped[int] = mapped_column(ForeignKey("products.product_id"))
|
||||||
purchase_id: Mapped[int] = mapped_column(ForeignKey("purchases.id"))
|
purchase_id: Mapped[int] = mapped_column(ForeignKey("purchases.id"))
|
||||||
|
|
||||||
product: Mapped[Product] = relationship(lazy="joined")
|
product: Mapped[Product] = relationship(back_populates='purchases', lazy="joined")
|
||||||
purchase: Mapped[Purchase] = relationship(lazy="joined")
|
purchase: Mapped[Purchase] = relationship(back_populates='entries', lazy="joined")
|
||||||
|
|
||||||
def __init__(self, purchase, product, amount):
|
def __init__(self, purchase, product, amount):
|
||||||
self.product = product
|
self.product = product
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ class Transaction(Base):
|
|||||||
purchase_id: Mapped[int | None] = mapped_column(ForeignKey("purchases.id"))
|
purchase_id: Mapped[int | None] = mapped_column(ForeignKey("purchases.id"))
|
||||||
|
|
||||||
user: Mapped[User] = relationship(lazy="joined")
|
user: Mapped[User] = relationship(lazy="joined")
|
||||||
purchase: Mapped[Purchase] = relationship(lazy="joined")
|
purchase: Mapped[Purchase] = relationship(back_populates='transactions', lazy="joined")
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from sqlalchemy import (
|
from sqlalchemy import (
|
||||||
@@ -14,19 +15,22 @@ from sqlalchemy.orm import (
|
|||||||
from .Base import Base
|
from .Base import Base
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .UserProducts import UserProducts
|
|
||||||
from .Transaction import Transaction
|
from .Transaction import Transaction
|
||||||
|
from .UserProducts import UserProducts
|
||||||
|
|
||||||
|
|
||||||
class User(Base):
|
class User(Base):
|
||||||
__tablename__ = "users"
|
__tablename__ = "users"
|
||||||
name: Mapped[str] = mapped_column(String(10), primary_key=True)
|
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))
|
card: Mapped[str | None] = mapped_column(String(20))
|
||||||
rfid: Mapped[str | None] = mapped_column(String(20))
|
rfid: Mapped[str | None] = mapped_column(String(20))
|
||||||
|
|
||||||
products: Mapped[list[UserProducts]] = relationship(back_populates="user")
|
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]+"
|
name_re = r"[a-z]+"
|
||||||
card_re = r"(([Nn][Tt][Nn][Uu])?[0-9]+)?"
|
card_re = r"(([Nn][Tt][Nn][Uu])?[0-9]+)?"
|
||||||
|
|||||||
@@ -27,5 +27,5 @@ class UserProducts(Base):
|
|||||||
count: Mapped[int] = mapped_column(Integer)
|
count: Mapped[int] = mapped_column(Integer)
|
||||||
sign: Mapped[int] = mapped_column(Integer)
|
sign: Mapped[int] = mapped_column(Integer)
|
||||||
|
|
||||||
user: Mapped[User] = relationship()
|
user: Mapped[User] = relationship(back_populates="products")
|
||||||
product: Mapped[Product] = relationship()
|
product: Mapped[Product] = relationship(back_populates="users")
|
||||||
|
|||||||
Reference in New Issue
Block a user