SQLAlchemy - getting a list of tables

I could not 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 tables.

+61
python pyramid mysql sqlalchemy
Jun 24 2018-11-21T00:
source share
8 answers

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

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

If you use a declarative extension, you probably do not manage the metadata yourself. Fortunately, metadata is still present in the base class,

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

If you are trying to figure out which tables are in your database, even among those that you have not even talked about SQLAlchemy about, then you can use table reflection. Then, SQLAlchemy will check the database and update the metadata with all the missing tables.

 >>> metadata.reflect(engine) 
+61
Jun 24 '11 at 21:40
source share

There is a method in the engine object to retrieve a list of table names. engine.table_names()

+37
May 31 '15 at 6:51
source share

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 executes and returns all tables.

Update:

Postgres:

 eng = create_engine('postgresql+psycopg2://root:password@localhost/ q = eng.execute('SELECT * FROM pg_catalog.pg_tables') 
+11
Sep 21 '16 at 19:05
source share
 from sqlalchemy import create_engine engine = create_engine('postgresql://use:pass@localhost/DBname') print (engine.table_names()) 
+8
Mar 18 '17 at 21:36
source share

A metadata object created using tables with has this in the dictionary.

 metadata.tables.keys() 
+4
Jun 24. 2018-11-21T00:
source share

Mirroring all tables at the same time also allows you to get hidden table names. I created some temporary tables and they appeared using

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

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

+2
Nov 23 '16 at 23:04
source share

I am solving the same problem and found this post. After some attempt to run, I would suggest using below to list all the tables: (mentioned by zerocog)

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

This is useful for direct processing of tables, and I believe that it is recommended.

And use the code below to get the table names:

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

"metadata.tables" provides a Dict for the table name and the Table object. which will also be useful for a quick request.

+2
May 10 '17 at 9:08
source share

In python interpreter use db.engine.table_names ()

 $ python >>> from myapp import db >>> db.engine.table_names() 
0
Dec 12 '18 at 19:23
source share



All Articles