fix: ADR-0035 4i(c) — don't pre-flag a self-referencing FK parent

A `CREATE TABLE` whose foreign key references the table being created
(`create table T (id int primary key, parent_id int references T(id))`)
parses and executes correctly, but the pre-submit schema-existence
diagnostic flagged the not-yet-created table as "no such table" — the FK
parent slot is `IdentSource::Tables`, and the target isn't in the schema
yet.

schema_existence_diagnostics now collects the CREATE TABLE target(s)
(`IdentSource::NewName`, role `table_name`) and exempts a `Tables`
reference matching one (case-insensitively) from the unknown-table flag.
A FK to a genuinely-unknown *other* table is still flagged.

Tests: self-ref FK not flagged; FK to an unknown other table still
flagged. Full suite 1915 passing / 0 failing / 1 ignored; clippy clean.
This commit is contained in:
claude@clouddev1
2026-05-26 12:20:30 +00:00
parent f85261032d
commit c2eb8cb982
2 changed files with 57 additions and 9 deletions
+6 -8
View File
@@ -150,14 +150,12 @@ pub(crate) static CHECK_NODES: &[Node] = &[
// endpoints; the `( <col> )` is optional (the bare `REFERENCES
// <parent>` form resolves to the parent's PK at execution).
// NOTE (4i): `IdentSource::Tables` existence-checks the parent — good
// for the common case (a typo'd parent shows a pre-submit hint), but a
// self-referencing FK (`references <self>` while creating `<self>`)
// false-flags the not-yet-created table as unknown. Parse + execution
// are correct (the self-ref is validated against the in-statement
// columns); only the live typing indicator is briefly wrong. ADR-0035
// §13 4i: teach the schema-existence diagnostic about the CREATE TABLE
// target so the self-ref indicator stops lying.
// `IdentSource::Tables` existence-checks the parent, so a typo'd parent
// shows a pre-submit hint. A self-referencing FK (`references <self>`
// while creating `<self>`) is NOT flagged: the schema-existence
// diagnostic exempts a `Tables` reference matching the `CREATE TABLE`
// target — the table being created in the same statement (ADR-0035 §4i c,
// `schema_existence_diagnostics`'s `created_tables`).
const FK_PARENT_TABLE: Node = Node::Ident {
source: IdentSource::Tables,
role: "fk_parent_table",