14

After a database is constructed in SQLAlchemy, I want to get all of the models, which is to say, the classes which represent them

>>> db
<SQLAlchemy engine='postgresql:///example'>
>>> ???
[<db.Model 'User'>, <db.Model 'Comment'>, <db.Model 'Article'>]

Can this be achieved? How?

I can get the tables decently, just not the models. Eg:

>>> for t in db.metadata.tables.items():
        print(t)

this gives the tables just not the models themselves

corvid
  • 10,733
  • 11
  • 61
  • 130

6 Answers6

14

There are several related questions, but I didn't see any exact duplicates.

Using your code to get the table names, and this question's accepted answer to get the classes, we can match the classes to the tablenames that are registered on your database.

classes, models, table_names = [], [], []
for clazz in db.Model._decl_class_registry.values():
    try:
        table_names.append(clazz.__tablename__)
        classes.append(clazz)
    except:
        pass
for table in db.metadata.tables.items():
    if table[0] in table_names:
        models.append(classes[table_names.index(table[0])])

Where models is the list of models registed on your database.

The try catch is required because a <sqlalchemy.ext.declarative.clsregistry._ModuleMarker object> will be included in the for clazz in ... loop, and it doesn't have a __tablename__ attribute.

Community
  • 1
  • 1
Celeo
  • 5,583
  • 8
  • 39
  • 41
11

If you only need the classes, there is an simpler solution. I came up with it based on Celeo’s answer:

from flask import current_app

# This is to be generic (ie. no need to pass your actual SQLAlchemy instance)
# This way you can include it even in your own extension.
db = current_app.extensions['sqlalchemy'].db

[cls for cls in db.Model._decl_class_registry.values()
 if isinstance(cls, type) and issubclass(cls, db.Model)]

This doesn’t need all those extra helper variables, just queries the class registry, and filters out everything that is not a model.

GergelyPolonkai
  • 6,230
  • 6
  • 35
  • 69
7

In SQLAlchemy 1.4, the _decl_class_registry.values() method has removed, you can use db.Model.registry.mappers instead:

models = {
    mapper.class_.__name__: mapper.class_
    for mapper in db.Model.registry.mappers
}

See the details in this issue.

Grey Li
  • 11,664
  • 4
  • 54
  • 64
0

this works in my case: slightly shorter; more readable

models = []
for name, thing in db_table_creation.__dict__.iteritems():
    if isinstance(thing, sqlalchemy.ext.declarative.DeclarativeMeta) and hasattr(thing, '__tablename__'):
        models.append(thing)
Berry Tsakala
  • 15,313
  • 12
  • 57
  • 80
0

Here is my variation of Celeo’s answer with typing and succinct intermediary steps:

from ??? import db  # Import from wherever you have this, or use GergelyPolonkai's method to get a db object.
from typing import List, Dict, Union
from flask_sqlalchemy import DefaultMeta
from sqlalchemy.ext.declarative.clsregistry import _ModuleMarker

registered_classes: List[Union[DefaultMeta, _ModuleMarker]] = \
    [x for x in db.Model._decl_class_registry.values()]
registered_models: List[DefaultMeta] = \
    [x for x in registered_classes if isinstance(x, DefaultMeta)]
tables: List[DefaultMeta] = \
    [x for x in registered_models if hasattr(x, '__tablename__')]
Joe Flack
  • 866
  • 8
  • 14
0

A simple way to do is:

import inspect
import mymodels

for name, obj in inspect.getmembers(mymodels):
    if inspect.isclass(obj) and hasattr(obj, '__tablename__'):
        print("model: %s %s"%(name,obj))
Adán Escobar
  • 1,729
  • 9
  • 15