From 1f3ef0d112bd05b8788176e08761f2afa86b2983 Mon Sep 17 00:00:00 2001 From: AnnushkaStark <130693211+AnnushkaStark@users.noreply.github.com> Date: Sat, 20 Jul 2024 01:35:20 +0300 Subject: [PATCH] added db models --- src/models/__init__.py | 5 +++++ src/models/beer.py | 41 +++++++++++++++++++++++++++++++++++++++++ src/models/category.py | 35 +++++++++++++++++++++++++++++++++++ src/models/m2m.py | 25 +++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 src/models/__init__.py create mode 100644 src/models/beer.py create mode 100644 src/models/category.py create mode 100644 src/models/m2m.py diff --git a/src/models/__init__.py b/src/models/__init__.py new file mode 100644 index 0000000..752e1f1 --- /dev/null +++ b/src/models/__init__.py @@ -0,0 +1,5 @@ +from .beer import Beer +from .category import Category +from .m2m import BeerCategory + +__all__ = ["Beer", "Category", "BeerCategory"] diff --git a/src/models/beer.py b/src/models/beer.py new file mode 100644 index 0000000..3fb59e2 --- /dev/null +++ b/src/models/beer.py @@ -0,0 +1,41 @@ +from typing import TYPE_CHECKING, List + +from sqlalchemy import Float, Integer, String +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from databases.database import Base + +from .m2m import BeerCategory + +if TYPE_CHECKING: + from .category import Category + + +class Beer(Base): + """ + Модель пивасика))) + + ## Attrs: + -id: int - идентификатор товара + - name: str - название товара + - brand: str - марка товара + - quantity: int -количество + - price: float - цена + - categories: List[Category] - связь + список категорий в которые входит товар + """ + + __tablename__ = "user" + id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) + name: Mapped[str] = mapped_column(String, unique=True) + brand: Mapped[str] + quantity: Mapped[int] + price: Mapped[float] = mapped_column(Float) + categories: Mapped[List["Category"]] = relationship( + "Category", + secondary=BeerCategory.__table__, + back_populates="beer", + ) + + def __repr__(self): + return f"{self.name}{self.price}" diff --git a/src/models/category.py b/src/models/category.py new file mode 100644 index 0000000..4b86f80 --- /dev/null +++ b/src/models/category.py @@ -0,0 +1,35 @@ +from typing import TYPE_CHECKING, List + +from sqlalchemy import Integer, String +from sqlalchemy.orm import Mapped, mapped_column, relationship + +from databases.database import Base + +from .m2m import BeerCategory + +if TYPE_CHECKING: + from .beer import Beer + + +class Category(Base): + """ + Модель категории + + ## Attrs: + - id: int - идентификатор категории + - name: str - название категории + - beer: List[Beer] - связь пиво в категории + """ + + __tablename__ = "catrgory" + + id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True) + name: Mapped[str] = mapped_column(String, unique=True) + beer: Mapped[List["Beer"]] = relationship( + "Beer", + secondary=BeerCategory.__table__, + back_populates="categories", + ) + + def __repr__(self): + return f"{self.name}" diff --git a/src/models/m2m.py b/src/models/m2m.py new file mode 100644 index 0000000..2bda8d6 --- /dev/null +++ b/src/models/m2m.py @@ -0,0 +1,25 @@ +from sqlalchemy import ForeignKey +from sqlalchemy.orm import Mapped, mapped_column + +from databases.database import Base + + +class BeerCategory(Base): + """ + Модель m2m связи пива и категории + + ## Attrs: + - category_id: int - идентификатор категории + - beer_id: int - идентифкатор пива + """ + + __tablename__ = "beer_category" + + category_id: Mapped[int] = mapped_column( + ForeignKey("city.id", ondelete="CASCADE"), + primary_key=True, + ) + beer_id: Mapped[int] = mapped_column( + ForeignKey("user.id", ondelete="CASCADE"), + primary_key=True, + )