Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Finding circular dependencies in your SQLAlchemy data scheme
from collections import defaultdict
dependencies = defaultdict(set)
#use your metadata object here
for t in metadata.sorted_tables:
for fkey in t.foreign_key_constraints:
dependencies[t.name].add(fkey.referred_table.name)
def find_circular_dependencies(dependencies,verbose = False):
def walk_graph(name,path = None,tabs = 0):
if path is None:
path = []
for dependency in dependencies[name]:
if verbose:
print " "*tabs,dependency
if dependency in path:
print "Circular dependency:","->".join(path+[dependency])
continue
walk_graph(dependency,path+[dependency],tabs = tabs+1)
for name in dependencies.keys():
walk_graph(name)
#will print your circular dependencies
find_circular_dependencies(dependencies)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.