мой код следующий:
Код: Выделить всё
src/__init__.py
Код: Выделить всё
from sqlmodel import create_engine, text
from sqlalchemy.ext.asyncio import AsyncEngine
from src.config import Config
engine = AsyncEngine(create_engine(
url=Config.DATABASE_URL,
echo=True
))
async def initdb():
"""create a connection to our db"""
async with engine.begin() as conn:
statement = text("select 'Hello World'")
result = await conn.execute(statement)
print(result)
Код: Выделить всё
src/db/main.py
Код: Выделить всё
from sqlmodel import create_engine, text
from sqlalchemy.ext.asyncio import AsyncEngine
from src.config import Config
engine = AsyncEngine(create_engine(
url=Config.DATABASE_URL,
echo=True
))
async def initdb():
"""create a connection to our db"""
async with engine.begin() as conn:
statement = text("select 'Hello World'")
result = await conn.execute(statement)
print(result)
Код: Выделить всё
.env
Код: Выделить всё
DATABASE_URL="postgresql+asyncpg://username:password@server_domain/fastapi?sslmode"
Код: Выделить всё
src/config.py
Код: Выделить всё
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
DATABASE_URL: str
model_config = SettingsConfigDict(
env_file=".env",
extra="ignore"
)
# add this line
Config = Settings()
InvalidRequestError: расширение asyncio требует наличия асинхронного драйвера. использовал. Загруженный
'psycopg2' не является асинхронным.
версии пакета (из требований.txt):
Код: Выделить всё
SQLAlchemy==2.0.36
sqlmodel==0.0.22
Любая помощь будет оценена по достоинству.
Подробнее здесь: https://stackoverflow.com/questions/793 ... the-loaded