added db models

This commit is contained in:
AnnushkaStark 2024-07-20 01:35:20 +03:00
parent 9f7029484b
commit 1f3ef0d112
4 changed files with 106 additions and 0 deletions

5
src/models/__init__.py Normal file
View File

@ -0,0 +1,5 @@
from .beer import Beer
from .category import Category
from .m2m import BeerCategory
__all__ = ["Beer", "Category", "BeerCategory"]

41
src/models/beer.py Normal file
View File

@ -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}"

35
src/models/category.py Normal file
View File

@ -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}"

25
src/models/m2m.py Normal file
View File

@ -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,
)