-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
75 lines (53 loc) · 2.63 KB
/
models.py
File metadata and controls
75 lines (53 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from __future__ import annotations
from datetime import date
from typing import List
from sqlalchemy import (
Date,
ForeignKey,
Integer,
Numeric,
String,
Text,
)
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
class Base(DeclarativeBase):
pass
class Product(Base):
__tablename__ = "products"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(200), nullable=False, index=True)
category: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
description: Mapped[str | None] = mapped_column(Text, nullable=True)
purchases: Mapped[List["Purchase"]] = relationship(
back_populates="product", cascade="all, delete-orphan"
)
sales: Mapped[List["Sale"]] = relationship(
back_populates="product", cascade="all, delete-orphan"
)
class Supplier(Base):
__tablename__ = "suppliers"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(200), nullable=False, index=True)
city: Mapped[str] = mapped_column(String(120), nullable=False, index=True)
address: Mapped[str | None] = mapped_column(Text, nullable=True)
purchases: Mapped[List["Purchase"]] = relationship(
back_populates="supplier", cascade="all, delete-orphan"
)
class Purchase(Base):
__tablename__ = "purchases"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
product_id: Mapped[int] = mapped_column(ForeignKey("products.id", ondelete="CASCADE"), nullable=False, index=True)
supplier_id: Mapped[int] = mapped_column(ForeignKey("suppliers.id", ondelete="CASCADE"), nullable=False, index=True)
purchase_date: Mapped[date] = mapped_column(Date, nullable=False)
quantity: Mapped[int] = mapped_column(Integer, nullable=False)
unit_cost: Mapped[float] = mapped_column(Numeric(12, 2), nullable=False)
product: Mapped["Product"] = relationship(back_populates="purchases")
supplier: Mapped["Supplier"] = relationship(back_populates="purchases")
class Sale(Base):
__tablename__ = "sales"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
product_id: Mapped[int] = mapped_column(ForeignKey("products.id", ondelete="CASCADE"), nullable=False, index=True)
sale_date: Mapped[date] = mapped_column(Date, nullable=False)
quantity: Mapped[int] = mapped_column(Integer, nullable=False)
unit_price: Mapped[float] = mapped_column(Numeric(12, 2), nullable=False)
product: Mapped["Product"] = relationship(back_populates="sales")