Skip to content

Instantly share code, notes, and snippets.

@adewes
Last active September 16, 2015 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adewes/dea76a0cc7c56c705d74 to your computer and use it in GitHub Desktop.
Save adewes/dea76a0cc7c56c705d74 to your computer and use it in GitHub Desktop.
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