I’m learning FastAPI from their official tutorial guide and I’ve reached currently at chapter 5 (Databases with SQLModel). I’m using neon db as stated in the docs.
my code are as follows:
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()
With the above code I’m getting InvalidRequestError:
InvalidRequestError: The asyncio extension requires an async driver to be used. The loaded
‘psycopg2’ is not async.
package versions are (from requirements.txt):
SQLAlchemy==2.0.36
sqlmodel==0.0.22
I’m running my python in virtual env in mac(m1). Does this error has to do anything with my hardware or they have changed anything in the package that they forgot to update in their tutorial docs.
Any kind of help will be appreciated.