Skip to content

SQL FK-reference edges use referencing-file stem instead of resolving to the defining file, producing dangling edges across multi-migration schemas #2442

Open

Description

@MG44CPA

Summary

When a SQL migration file has a foreign-key reference to a table defined in an earlier migration file, the extractor stamps the target node ID using the referencing file's stem instead of resolving to the table's actual defining file. This produces edges pointing at node IDs that don't exist anywhere in the extraction, showing up as dangling-endpoint edges in graphify.diagnostics.diagnose_extraction.

Repro

Given two migrations:

-- 001_initial_schema.sql
create table public.profiles (id uuid primary key, ...);
-- 008_expansion_tables.sql
create table public.household_members (
  profile_id uuid references public.profiles(id),
  ...
);

The extractor correctly creates:

  • supabase_migrations_001_initial_schema_public_profiles (the real profiles table node)

But the FK reference edge from 008_expansion_tables.sql targets:

  • 008_expansion_tables_public_profiles (does not exist — wrong file stem)

instead of resolving to supabase_migrations_001_initial_schema_public_profiles.

Impact

On a real project (24 SQL migration files, Supabase/Postgres), this produced 92 dangling edges — every migration that FKs into a table defined in an earlier file is affected (observed in 004, 005, 008, 013, 018, 021, 022 referencing tables from 001 and 008). All 92 are legitimate schema relationships; none are false edges — the extractor just can't resolve the correct target node ID across files.

Expected behavior

The SQL extractor should maintain a project-wide symbol table (table name → defining file/node ID) built during the AST pass, and resolve references public.<table>(...) targets against that table rather than stamping the target ID with the local file's stem.

Environment

  • graphifyy 0.9.30
  • tree_sitter_sql installed via graphifyy[sql]
  • Reproduced via graphify.diagnostics.diagnose_extraction(extraction, directed=False, root='.') on a 24-file Supabase migrations folder

Activity

  1. kreativekubedigital-creator commented on Sep 23, 2026

    @kreativekubedigital-creator

    Corroborating this from a second Supabase/Postgres corpus, with a different observable symptom from the same root cause — worth recording here because it changes how big the fix's payoff is.

    Setup: graphifyy 0.9.65, tree-sitter-sql 0.3.11, 106 SQL migrations + 27 Markdown docs, built via the /graphify skill (undirected).

    In my run the cross-file targets mostly did exist as nodes — only 9 dangling edges, not the 92 you saw. Instead the extractor materialised a separate node per file for the same schema object:

    Node count Label
    26× public.<table_a>
    22× public.<table_b>
    21× public.<table_c> / public.<table_d>
    17× public.<table_e> / public.<table_f>

    So rather than one dangling edge, you get N disconnected islands. The resulting graph:

    • 1,203 nodes, 1,626 edges — but 127 connected components, the largest holding only 172 nodes
    • 1,031 of 1,203 nodes sit outside the largest component
    • One table appeared in components of size 39, 19 and 17 — same table, three nodes, no edge between them

    Effectively the SQL half becomes 106 per-file graphs rather than one schema graph, so "what touches <table>?" has N disconnected answers.

    Why I think it's the same bug: both symptoms come from schema-object identity being stamped with the local file's stem. Whether you get a dangling edge or a duplicate node just depends on whether the referencing file also happens to emit a stub for that object. The project-wide symbol table you propose (table name → defining file/node ID) fixes both — that's a larger payoff than the dangling-edge count alone suggests.

    Knock-on effect on the report layer. Community detection inherits the fragmentation: 146 communities, most of them "one migration file", with cohesion scores of 0.05–0.08. GRAPH_REPORT.md then generated "Should <community> be split into smaller, more focused modules?" — a refactor suggestion aimed at a module that does not exist, where the low cohesion is entirely an artefact of the duplicate nodes. That is the same failure mode as #446, but arriving through identity rather than through Leiden's resolution parameter, so tuning clustering would not fix this instance.

    Related: #207 describes this generically ("the same real-world concept can appear as multiple graph nodes… related neighborhoods split across disconnected components… distorted centrality, clustering, and query results"); this is its concrete SQL-schema case.

    Happy to supply the diagnostic output if useful — diagnose_extraction reported 9 dangling, 0 missing, 0 self-loops, 213 undirected same-endpoint collapsed edges across 117 groups.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions