'Reorder to make things possible. Reordering may be needed so FKs work
nicely inside the same app.'

def _sort_migrations(self):

    for (app_label, ops) in sorted(self.generated_operations.items()):
        dependency_graph = {op: set() for op in ops}
        for op in ops:
            for dep in op._auto_deps:
                if (dep[0] == app_label):
                    for op2 in ops:
                        if self.check_dependency(op2, dep):
                            dependency_graph[op].add(op2)
        self.generated_operations[app_label] = stable_topological_sort(ops, dependency_graph)
