-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathstuff.py
43 lines (35 loc) · 1.49 KB
/
stuff.py
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
import uuid
from sqlalchemy import ForeignKey, String, select
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Mapped, joinedload, mapped_column, relationship
from app.models.base import Base
from app.models.nonsense import Nonsense
from app.utils.decorators import compile_sql_or_scalar
class Stuff(Base):
__tablename__ = "stuff"
__table_args__ = ({"schema": "happy_hog"},)
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True
)
name: Mapped[str] = mapped_column(String, primary_key=True, unique=True)
description: Mapped[str | None]
nonsense: Mapped["Nonsense"] = relationship(
"Nonsense", secondary="happy_hog.stuff_full_of_nonsense"
)
@classmethod
@compile_sql_or_scalar
async def find(cls, db_session: AsyncSession, name: str, compile_sql=False):
stmt = select(cls).options(joinedload(cls.nonsense)).where(cls.name == name)
return stmt
class StuffFullOfNonsense(Base):
__tablename__ = "stuff_full_of_nonsense"
__table_args__ = ({"schema": "happy_hog"},)
id: Mapped[uuid.UUID] = mapped_column(
UUID(as_uuid=True), default=uuid.uuid4, primary_key=True
)
stuff_id: Mapped[Stuff] = mapped_column(UUID, ForeignKey("happy_hog.stuff.id"))
nonsense_id: Mapped["Nonsense"] = mapped_column(
UUID, ForeignKey("happy_hog.nonsense.id")
)
but_why: Mapped[str | None]