db class
This commit is contained in:
54
app/database.py
Normal file
54
app/database.py
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import datetime as dt
|
||||||
|
|
||||||
|
from sqlalchemy import BigInteger, DateTime, ForeignKey, create_engine, func
|
||||||
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, sessionmaker
|
||||||
|
|
||||||
|
|
||||||
|
class Base(DeclarativeBase): ...
|
||||||
|
|
||||||
|
|
||||||
|
class User(Base):
|
||||||
|
__tablename__ = "user"
|
||||||
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||||
|
fullname: Mapped[str] = mapped_column()
|
||||||
|
tg_id: Mapped[int] = mapped_column(BigInteger)
|
||||||
|
phone: Mapped[str] = mapped_column(nullable=True)
|
||||||
|
|
||||||
|
|
||||||
|
class Event(Base):
|
||||||
|
__tablename__ = "event"
|
||||||
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||||
|
title: Mapped[str] = mapped_column()
|
||||||
|
description: Mapped[str] = mapped_column(default="")
|
||||||
|
start_date: Mapped[dt.date] = mapped_column(nullable=True)
|
||||||
|
end_date: Mapped[dt.date] = mapped_column(nullable=True)
|
||||||
|
|
||||||
|
|
||||||
|
class UserEvent(Base):
|
||||||
|
__tablename__ = "user_event"
|
||||||
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||||
|
user: Mapped[int] = mapped_column(ForeignKey("user.id"))
|
||||||
|
event: Mapped[int] = mapped_column(ForeignKey("event.id"))
|
||||||
|
date: Mapped[dt.date] = mapped_column(
|
||||||
|
DateTime(timezone=True), server_default=func.now()
|
||||||
|
)
|
||||||
|
status: Mapped[str] = mapped_column(default="not confirmed")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
engine = create_engine("sqlite:///test.db")
|
||||||
|
SessionMaker = sessionmaker(engine)
|
||||||
|
Base.metadata.drop_all(engine)
|
||||||
|
Base.metadata.create_all(engine)
|
||||||
|
|
||||||
|
with SessionMaker() as session:
|
||||||
|
user = User(fullname = "Shevkunov Daniil", tg_id=12023)
|
||||||
|
event = Event(title="Event")
|
||||||
|
|
||||||
|
session.add_all([user, event])
|
||||||
|
session.flush()
|
||||||
|
user_event = UserEvent(user=user.id, event=event.id)
|
||||||
|
session.add(user_event)
|
||||||
|
session.commit()
|
||||||
|
# with SessionMaker as session:
|
||||||
|
|
||||||
|
|
||||||
0
app/main.py
Normal file
0
app/main.py
Normal file
Reference in New Issue
Block a user