'Sorts tables in constraint order, least constrained tables first
Constructs a constraint graph for the tables in the supplied database
or databases directory, sorts it in topological order with the least
constained tables first, and returns the table names in that order in
a list.'

def order_tables(self):

    self.add_all_tables_to_graph()
    ordered_tables = None
    try:
        ordered_tables = nx.topological_sort(self.graph)
    except:
        ordered_tables = None
    if (not ordered_tables):
        cycles = nx.cycle_basis(nx.Graph(self.graph))
        errmsg = (('Tables    and    their    constraints    do    not    form    a    DAG.\n' + 'Found    possible    cycles:\n') + str(cycles))
        raise ValueError, errmsg
    return ordered_tables
