2024-05-25 16:11:52 +04:00
|
|
|
from typing import Optional
|
|
|
|
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
|
|
|
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
|
|
|
2024-05-25 16:51:34 +04:00
|
|
|
engine = create_async_engine("sqlite+aiosqlite:///questions.db")
|
2024-05-25 16:11:52 +04:00
|
|
|
new_session = async_sessionmaker(engine, expire_on_commit=False)
|
|
|
|
|
|
|
|
class Model(DeclarativeBase):
|
|
|
|
pass
|
|
|
|
|
2024-05-25 16:51:34 +04:00
|
|
|
class QuestionOrm(Model):
|
|
|
|
__tablename__ = "questions"
|
2024-05-25 16:11:52 +04:00
|
|
|
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True)
|
2024-05-25 16:51:34 +04:00
|
|
|
id_user: Mapped[int]
|
|
|
|
type_question: Mapped[bool]
|
|
|
|
question: Mapped[str]
|
|
|
|
answer: Mapped[Optional[str]]
|
2024-05-25 16:11:52 +04:00
|
|
|
|
|
|
|
async def create_tables():
|
|
|
|
async with engine.begin() as conn:
|
|
|
|
await conn.run_sync(Model.metadata.create_all)
|
|
|
|
|
|
|
|
async def delete_tables():
|
|
|
|
async with engine.begin() as conn:
|
|
|
|
await conn.run_sync(Model.metadata.drop_all)
|