From 896bc4c5b6616492c113d80c1328032413d22b5e Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 1 Dec 2011 14:21:43 -0500 Subject: - [bug] Fixed bug whereby "order_by='foreign_key'" option to Inspector.get_table_names wasn't implementing the sort properly, replaced with the existing sort algorithm - clean up metadata usage in reflection tests --- lib/sqlalchemy/engine/reflection.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) (limited to 'lib/sqlalchemy/engine/reflection.py') diff --git a/lib/sqlalchemy/engine/reflection.py b/lib/sqlalchemy/engine/reflection.py index e87c1ce0f..a69fc5b98 100644 --- a/lib/sqlalchemy/engine/reflection.py +++ b/lib/sqlalchemy/engine/reflection.py @@ -27,6 +27,7 @@ methods such as get_table_names, get_columns, etc. import sqlalchemy from sqlalchemy import exc, sql from sqlalchemy import util +from sqlalchemy.util import topological from sqlalchemy.types import TypeEngine from sqlalchemy import schema as sa_schema @@ -150,27 +151,19 @@ class Inspector(object): if hasattr(self.dialect, 'get_table_names'): tnames = self.dialect.get_table_names(self.bind, - schema, - info_cache=self.info_cache) + schema, info_cache=self.info_cache) else: tnames = self.engine.table_names(schema) if order_by == 'foreign_key': - ordered_tnames = tnames[:] - # Order based on foreign key dependencies. + import random + random.shuffle(tnames) + + tuples = [] for tname in tnames: - table_pos = tnames.index(tname) - fkeys = self.get_foreign_keys(tname, schema) - for fkey in fkeys: - rtable = fkey['referred_table'] - if rtable in ordered_tnames: - ref_pos = ordered_tnames.index(rtable) - # Make sure it's lower in the list than anything it - # references. - if table_pos > ref_pos: - ordered_tnames.pop(table_pos) # rtable moves up 1 - # insert just below rtable - ordered_tnames.index(ref_pos, tname) - tnames = ordered_tnames + for fkey in self.get_foreign_keys(tname, schema): + if tname != fkey['referred_table']: + tuples.append((tname, fkey['referred_table'])) + tnames = list(topological.sort(tuples, tnames)) return tnames def get_table_options(self, table_name, schema=None, **kw): -- cgit v1.2.1