diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-12-01 14:21:43 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2011-12-01 14:21:43 -0500 |
commit | 896bc4c5b6616492c113d80c1328032413d22b5e (patch) | |
tree | 6d1876f580725fe05a097b89c2848724195d7465 /lib/sqlalchemy/engine/reflection.py | |
parent | bd8e93e19fd0b353a70c22a3ff1dc15528205b06 (diff) | |
download | sqlalchemy-896bc4c5b6616492c113d80c1328032413d22b5e.tar.gz |
- [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
Diffstat (limited to 'lib/sqlalchemy/engine/reflection.py')
-rw-r--r-- | lib/sqlalchemy/engine/reflection.py | 27 |
1 files changed, 10 insertions, 17 deletions
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): |