Skip to content

FK cycles & self-references

Problem: your schema has a self-referencing table (employees.manager_id) or a cycle (a → b → a). Naive generation can’t satisfy the FKs.

Solution: nothing to configure — just generate:

Terminal window
dbsprout generate

Why it works: DBSprout builds a directed FK dependency graph and topologically sorts it. Self-referencing FKs are separated out. If true cycles exist, it finds the strongly-connected components (Tarjan’s algorithm), picks a nullable FK to defer, and does a two-pass insert: first pass writes rows with the deferred FK NULL, second pass updates them with real parent keys. The result is 100% FK integrity with no manual ordering.