dbt "Compilation Error: found a cycle" circular ref in CI
dbt topologically sorts the model graph and found a cycle: model A refs B and B refs A (directly or through a chain). A DAG cannot contain a cycle, so dbt refuses to build.
What this error means
dbt compile, run, or build fails with "Compilation Error ... Found a cycle" and names the models that form the loop.
dbt
Compilation Error
Found a cycle: model.my_project.orders --> model.my_project.customers -->
model.my_project.ordersCommon causes
Two models reference each other
A direct ref() in each of two models forms a two-node cycle that has no valid build order.
A longer ref chain closes back on itself
A model transitively refs an upstream model that in turn refs it, creating a loop across several files.
How to fix it
Break the cycle in the ref graph
- Read the cycle dbt prints to see which models close the loop.
- Remove or redirect one
ref()so the graph is acyclic. - Introduce an intermediate model if both directions are truly needed.
Terminal
dbt ls --select +orders --output pathRestructure shared logic
Extract the common upstream into a separate model that both models ref, instead of referencing each other.
How to prevent it
- Keep the model DAG acyclic; never let two models ref each other.
- Extract shared logic into an upstream model both can ref.
- Run
dbt compilein CI so a new cycle is caught before merge.
Related guides
dbt Compilation Error: model depends on a node not found (ref) in CIFix dbt "Compilation Error: Model X depends on a node named Y which was not found" in CI - a ref() points at…
dbt "Compilation Error: ambiguous" duplicate model name in CIFix dbt "Compilation Error ... found two resources with the name" in CI - two models, seeds, or sources share…
dbt "Compilation Error: X is undefined" Jinja var in CIFix dbt "Compilation Error ... 'X' is undefined" in CI - a model references a Jinja variable or var() that is…