sync: migrate secure-online-shop to Gitea (2026-08-10)
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from decimal import Decimal
|
||||
|
||||
from sqlalchemy import CheckConstraint, ForeignKey, Numeric
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.models.base import Base
|
||||
|
||||
|
||||
class OrderItem(Base):
|
||||
__tablename__ = "order_items"
|
||||
__table_args__ = (
|
||||
CheckConstraint("quantity > 0", name="ck_order_items_quantity_positive"),
|
||||
CheckConstraint("unit_price > 0", name="ck_order_items_unit_price_positive"),
|
||||
CheckConstraint("subtotal >= 0", name="ck_order_items_subtotal_non_negative"),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
order_id: Mapped[int] = mapped_column(ForeignKey("orders.id"), nullable=False, index=True)
|
||||
product_id: Mapped[int] = mapped_column(ForeignKey("products.id"), nullable=False, index=True)
|
||||
quantity: Mapped[int] = mapped_column(nullable=False)
|
||||
unit_price: Mapped[Decimal] = mapped_column(Numeric(10, 2), nullable=False)
|
||||
subtotal: Mapped[Decimal] = mapped_column(Numeric(10, 2), nullable=False)
|
||||
|
||||
order = relationship("Order", back_populates="items")
|
||||
product = relationship("Product", lazy="joined")
|
||||
|
||||
@property
|
||||
def product_name(self) -> str:
|
||||
return self.product.name
|
||||
Reference in New Issue
Block a user