igorbenav / fastcrud

FastCRUD is a Python package for FastAPI, offering robust async CRUD operations and flexible endpoint creation utilities.
MIT License
643 stars 45 forks source link

Error : "AttributeError: 'AsyncEngine' object has no attribute '_run_ddl_visitor'" #11

Closed serge-eric-kalaga closed 8 months ago

serge-eric-kalaga commented 8 months ago

Error initializing database Here is a piece of code that relates to the error

async def init_db(engine = engine):
    async with async_session() as session:
        Base.metadata.create_all(engine)

AttributeError: 'AsyncEngine' object has no attribute '_run_ddl_visitor'

How can I resolve this error?

serge-eric-kalaga commented 8 months ago

I finally found a solution. Here is the updated code

import asyncio

DATABASE_URL = "sqlite+aiosqlite:///./test.db"
engine = create_async_engine(DATABASE_URL, echo=True)
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)

async def create_tables():
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)

loop = asyncio.get_event_loop()
loop.run_until_complete(create_tables())
loop.close()