find dependent tables

Issue #323 resolved
Former user created an issue

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)

  1. Former user Account Deleted

    (original author: anonymous) Note: The table passed in may somehome be dependent on itself and could be present in the returned list.

  2. Former user Account Deleted

    AttributeError: 'module' object has no attribute 'TableCollection' Should this still be in wiki?

  3. Mike Bayer repo owner

    its a little bit of a pointless example but I modernized it and moved it out of the "user defined" section.

  4. Log in to comment