I'm doing this:
sengine = create_enging(blah..blah)
smeta = MetaData(bind=sengine)
meta.reflect(sengine)
tables = smeta.tables.keys()
to get a list of tables in a database, but unfortunately the table
list are not in their dependency order. For example:
A is depending on B (has a foreign key reference to B), B is
depending on C, the correct table list should be [A,B,C]. Yet the
smeta.tables.keys() will only return a unsorted list. Is there an API
or a code snippet to show me how to sort the returned list on their
dependency order ?
I have this dumb code snippet,which I think is really inefficient.
sorted_table = []
#===========================================================================
# sort all table list on their dependency order
#===========================================================================
while len(sorted_table) != len(tables):
for table_name in tables:
if table_name in sorted_table:
continue
table = Table(table_name, smeta, autoload=True)
if len(table.foreign_keys) == 0:
sorted_table.append(table_name)
else:
for foreign_key in table.foreign_keys:
if foreign_key.target_fullname.split(".")[0] in
sorted_table:
pass
else:break
else:
sorted_table.append(table_name)
Thanks && Regards
Tony
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.