Skip to content

SQL: ALTER TABLE ADD CONSTRAINT FK does not emit reference edge #1443

Description

@Widthdom

Summary

SQL reference extraction is asymmetric: CREATE TABLE … REFERENCES other_table(col) correctly emits a reference edge from the column to the referenced table, but ALTER TABLE … ADD CONSTRAINT … FOREIGN KEY (…) REFERENCES other_table(col) does not. Both forms are common in migration scripts; an ALTER-style foreign key should be tracked the same way.

Evidence

  • src/CodeIndex/Indexer/Extraction/LanguageReferenceExtraction.Sql.cs — the REFERENCES clause handler is wired into the CREATE TABLE parser but not the ALTER TABLE parser.

  • Reproducer:

    CREATE TABLE orders (id int PRIMARY KEY);
    CREATE TABLE order_items (order_id int);
    ALTER TABLE order_items ADD CONSTRAINT fk_order
      FOREIGN KEY (order_id) REFERENCES orders(id);

    After indexing, references on the orders table will miss the ALTER-created edge.

Impact

  • Schemas managed by migration tools (Flyway, Liquibase, golang-migrate) typically use ALTER TABLE ADD CONSTRAINT instead of inline REFERENCES. The asymmetry means impact_analysis on table dependencies is incomplete for those projects.

Proposed direction

  • Lift the REFERENCES clause parser into a shared helper used by both CREATE TABLE and ALTER TABLE … ADD CONSTRAINT.
  • Add a unit test covering both forms.

Repro env

  • Branch: main @ 2ee912d (release v1.21.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions