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)
Summary
SQL reference extraction is asymmetric:
CREATE TABLE … REFERENCES other_table(col)correctly emits a reference edge from the column to the referenced table, butALTER 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— theREFERENCESclause handler is wired into theCREATE TABLEparser but not theALTER TABLEparser.Reproducer:
After indexing,
referenceson theorderstable will miss the ALTER-created edge.Impact
ALTER TABLE ADD CONSTRAINTinstead of inlineREFERENCES. The asymmetry meansimpact_analysison table dependencies is incomplete for those projects.Proposed direction
REFERENCESclause parser into a shared helper used by bothCREATE TABLEandALTER TABLE … ADD CONSTRAINT.Repro env
main@ 2ee912d (release v1.21.0)