Skip to content

Instantly share code, notes, and snippets.

@notypecheck
Created December 19, 2024 01:03
Show Gist options
  • Save notypecheck/418a07e7dfaa34379f2149a517bfc12a to your computer and use it in GitHub Desktop.
Save notypecheck/418a07e7dfaa34379f2149a517bfc12a to your computer and use it in GitHub Desktop.
Single file script to debug and experiment with sqlalchemy
import asyncio
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
engine = create_async_engine("sqlite+aiosqlite:///:memory:", echo=True)
session_factory = async_sessionmaker(bind=engine)
class Base(DeclarativeBase):
pass
class Book(Base):
__tablename__ = "book"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str]
async def main() -> None:
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async with session_factory.begin() as session:
book = Book(name="Test")
session.add(book)
if __name__ == "__main__":
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment