SQLAlchemy - Getting a list of tables

PythonMysqlSqlalchemyPyramid

Python Problem Overview


I couldn't find any information about this in the documentation, but how can I get a list of tables created in SQLAlchemy?

I used the class method to create the tables.

Python Solutions


Solution 1 - Python

All of the tables are collected in the tables attribute of the SQLAlchemy MetaData object. To get a list of the names of those tables:

>>> metadata.tables.keys()
['posts', 'comments', 'users']

If you're using the declarative extension, then you probably aren't managing the metadata yourself. Fortunately, the metadata is still present on the baseclass,

>>> Base = sqlalchemy.ext.declarative.declarative_base()
>>> Base.metadata
MetaData(None)

If you are trying to figure out what tables are present in your database, even among the ones you haven't even told SQLAlchemy about yet, then you can use table reflection. SQLAlchemy will then inspect the database and update the metadata with all of the missing tables.

>>> metadata.reflect(engine)

For Postgres, if you have multiple schemas, you'll need to loop thru all the schemas in the engine:

from sqlalchemy import inspect
inspector = inspect(engine)
schemas = inspector.get_schema_names()

for schema in schemas:
    print("schema: %s" % schema)
    for table_name in inspector.get_table_names(schema=schema):
        for column in inspector.get_columns(table_name, schema=schema):
            print("Column: %s" % column)

Solution 2 - Python

There is a method in engine object to fetch the list of tables name. engine.table_names()

Solution 3 - Python

from sqlalchemy import create_engine
engine = create_engine('postgresql://use:pass@localhost/DBname')
print (engine.table_names())

Solution 4 - Python

  • To get a list of all existing tables in DB:

As of SQLAlchemy 1.4: https://docs.sqlalchemy.org/en/14/core/reflection.html#fine-grained-reflection-with-inspector

from sqlalchemy import create_engine
from sqlalchemy import inspect
engine = create_engine('...')
insp = inspect(engine)
print(insp.get_table_names())

Older methods (engine.table_names()) yield:

SADeprecationWarning: The from_engine() method on Inspector is deprecated and will be removed in a future release. Please use the sqlalchemy.inspect() function on an Engine or Connection in order to acquire an Inspector. (deprecated since: 1.4)

  • To get a list of declared tables, use accepted answer: metadata.tables.keys()

Solution 5 - Python

Within the python interpreter use db.engine.table_names()

$ python
>>> from myapp import db
>>> db.engine.table_names()

Solution 6 - Python

I was looking for something like this:

from sqlalchemy import create_engine
eng = create_engine('mysql+pymysql://root:password@localhost:3306', pool_recycle=3600)
q = eng.execute('SHOW TABLES')

available_tables = q.fetchall()

It does an execute and returns all of the tables.

update:

Postgres:

eng = create_engine('postgresql+psycopg2://root:password@localhost/
q = eng.execute('SELECT * FROM pg_catalog.pg_tables')

Solution 7 - Python

Just this simple:

engine.table_names()

Also, to test whether a table exists:

engine.has_table(table_name)

Solution 8 - Python

The metadata object that you created the tables with has that in a dictionary.

metadata.tables.keys()

Solution 9 - Python

I'm solving same problem and found this post. After some try run, I would suggest use below to list all tables: (mentioned by zerocog)

metadata = MetaData()
metadata.reflect(bind=engine)
for table in metadata.sorted_tables:
    print(table)

This is useful for direct table handling and I feel is recommended.

And use below code to get table names:

for table_name in engine.table_names():
    print(table_name)

"metadata.tables" provides a Dict for table name and Table object. which would also be useful for quick query.

Solution 10 - Python

Reflecting All Tables at Once allows you to retrieve hidden table names too. I created some temporary tables and they showed up with

meta = MetaData()
meta.reflect(bind=myengine)
for table in reversed(meta.sorted_tables):
    print table

Reference http://docs.sqlalchemy.org/en/latest/core/reflection.html

Solution 11 - Python

This is what I'm using as of 2021-10-22:

import sqlalchemy as sql

engine = sql.create_engine("connection_string")

sql.inspect(engine).get_table_names()

Solution 12 - Python

If you are using Flask-SQLAlchemy, you can get them from your db instance

[...]
db = SQLAlchemy(app)
db.engine.table_names() # you'll get a list of all the table names in your database

Solution 13 - Python

The best way is to use inspect:

  1. Create the inspector and connect it to the engine
  2. Collect the names of tables within the database
  3. Collect Table columns names
from sqlalchemy import create_engine, inspect

engine = create_engine("sqlite:///../Resources/dow.sqlite")
conn = engine.connect()
inspector = inspect(conn)
inspector.get_table_names() #returns "dow"

columns = inspector.get_columns('dow')

for column in columns:
    print(column["name"], column["type"])

Solution 14 - Python

Complete example of displaying all column information. Assumes variable df contains a dataframe to be written to the SQL database.

from sqlalchemy import create_engine, inspect
from sqlalchemy_utils.functions import database_exists, create_database

engine = create_engine('sqlite:///mydb.sqlite', echo=True)

if not database_exists(engine.url):
    create_database(engine.url)
else:
    engine.connect()

df.to_sql('MyTable', con=engine, if_exists='replace', index=False) # index=False avoids auto-creation of level_0 (name tiebreaker)

inspector = inspect(engine)
table_names = inspector.get_table_names()
for table_name in table_names:
    print(f"Table:{table_name}")
    column_items = inspector.get_columns(table_name)
    print('\t'.join(n for n in column_items[0]))
    for c in column_items:
        assert len(c) == len(column_items[0])
        print('\t'.join(str(c[n]) for n in c))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsidewinderView Question on Stackoverflow
Solution 1 - PythonSingleNegationEliminationView Answer on Stackoverflow
Solution 2 - PythonZubair AlamView Answer on Stackoverflow
Solution 3 - PythonMaedaView Answer on Stackoverflow
Solution 4 - PythonJean MonetView Answer on Stackoverflow
Solution 5 - PythonTim TrustonView Answer on Stackoverflow
Solution 6 - PythonjmunschView Answer on Stackoverflow
Solution 7 - PythonHan ZhangView Answer on Stackoverflow
Solution 8 - PythonKeithView Answer on Stackoverflow
Solution 9 - Pythonuser2189731View Answer on Stackoverflow
Solution 10 - PythonzerocogView Answer on Stackoverflow
Solution 11 - PythonMatt DanchoView Answer on Stackoverflow
Solution 12 - PythonNasser AbdouView Answer on Stackoverflow
Solution 13 - PythonRyanAbnaviView Answer on Stackoverflow
Solution 14 - PythonBSalitaView Answer on Stackoverflow