find dependent tables
Issue #323
resolved
Here is an implementation for a method to find all the tables that are dependent on some table. This may not be the world's best implementation but it works.
def find_dependent_tables(table, metadata=None):
if metadata is None:
metadata = default_metadata
result = []
def _impl(t2):
for name, t in metadata.tables.iteritems():
for c in t.c:
try:
if c.foreign_key.column.table == t2:
if t not in result:
result.append(t)
_impl(t)
except AttributeError, e:
pass
_impl(table)
collection = sql_util.TableCollection()
for r in result:
collection.add(r)
return collection.sort(False)
Comments (6)
-
Account Deleted -
repo owner - marked as minor
can we move this to the wiki as a usage recipe ?
-
Account Deleted it's already there...
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DependentTables
...and i think you even edited it ;)
-
repo owner - changed status to resolved
-
Account Deleted AttributeError: 'module' object has no attribute 'TableCollection' Should this still be in wiki?
-
repo owner its a little bit of a pointless example but I modernized it and moved it out of the "user defined" section.
- Log in to comment
(original author: anonymous) Note: The table passed in may somehome be dependent on itself and could be present in the returned list.