SQLAlchemy Runtime Inspection API doesn't support `AsyncEngine`

See original GitHub issue

The title pretty much describes this feature request. I would greatly appreciate if SQLAlchemy would support the Runtime Inspection API with a AsyncEngine much in the same way that it does with normal Engine objects. I know that the SQLAlchemy devs are very busy atm, so please don’t feel any pressure to implement this soon. Just wanted to let you guys know about this.

Have a nice day!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:19 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
Ahsokacommented, Jul 12, 2021

we can likely add this at some point and it can be sqlalchemy.ext.asyncio.reflection. it would cover the Inspector API using proxies and for per-dialect inspectors would use a __getattr__ scheme.

for now using run_sync() is the workaround.

Hello @whg517,

As @zzzeek mentioned above the best solution would be to use the run_sync() method. Consider the following example:

@pytest.mark.asyncio
async def test_migrate(session):
    """
    Test migrate.
    :param session:
    :return:
    """
    def get_table_names(conn):
        inspector = inspect(conn)
        return inspector.get_table_names()

    async with session.bind.connect() as connection:
        table_names = await connection.run_sync(get_table_names)
        assert table_names
        assert 'audit' in table_names

Learn more about the run_sync method here: https://docs.sqlalchemy.org/en/latest/orm/extensions/asyncio.html#sqlalchemy.ext.asyncio.AsyncConnection.run_sync

2reactions
harshitsinghai77commented, Oct 11, 2021

Update:

I got the workaround…

engine = create_async_engine(url)

def load_table(conn):
        metadata = MetaData(bind=conn)
        metadata.reflect(only=["harshit_table"])
        harshit_table = Table("harshit_table", metadata, autoload_with=engine)
        return harshit_table

async def async_main(self):
        async with engine.connect() as conn:
            table = await conn.run_sync(load_table)
            print("table: ", table, type(table))

or you can use it like this

metadata = MetaData()

async def async_main(self):
        async with engine.connect() as conn:
            await conn.run_sync(metadata.reflect, only=["harshit_table"])
            harshit_table = Table("harshit_table", metadata, autoload_with=engine)
            print("tables: ", harshit_table, type(harshit_table))

Don’t create metadata and use it like.

engine = create_async_engine(url)
metadata = MetaData(bind=engine)

my_table = Table("harshit_table", metadata, autoload_with=engine)

This won’t work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asynchronous I/O (asyncio)
The SQLAlchemy event system is not directly exposed by the asyncio extension, meaning there is not yet an “async” version of a SQLAlchemy...
Read more >
SQLAlchemy async engine with ORM unable to execute ...
Returns error sqlalchemy.exc.NoInspectionAvailable: Inspection on an AsyncConnection is currently not supported. Please use ``run_sync`` to ...
Read more >
pytest-async-sqlalchemy
pytest-async-sqlalchemy. PyPI version Python versions. Database testing fixtures using the SQLAlchemy asyncio API. You can install "pytest-async-sqlalchemy" ...
Read more >
sqlalchemy: the async-ening - matt.sh
The new AsyncEngine and AsyncSession also means you can now use the great asyncpg library for the async engine driver instead of being...
Read more >
Async SQLAlchemy with FastAPI
SQLAlchemy unifies Core and ORM APIs for consistency. Both Core and ORM now support async with asyncio, but this feature is not production ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found