Commit 8ea005a
committed
Foreign keys: full inline + table-level CREATE TABLE grammar (
[CONSTRAINT name] REFERENCES other(col) on the column being declared / [CONSTRAINT name] FOREIGN KEY (cols) REFERENCES other(cols)) with the complete ON DELETE / ON UPDATE action quartet — NO ACTION (default) / CASCADE / SET NULL / SET DEFAULT — backed by a new Storage/ForeignKey.cs (name + object id + both tables + child/referenced full-ordinal arrays + two ReferentialAction enum values matching sys.foreign_keys codes 0-3 + IsSystemNamed) and two new mutable lists on HeapTable (OutgoingForeignKeys child-side / IncomingForeignKeys parent-side) wired symmetrically by ResolveForeignKeys after the table is in its schema dict so self-references resolve. Runtime enforcement in a new Simulation.ForeignKeys.cs: EnforceOutgoingForeignKeys walks INSERT/UPDATE/MERGE post-rows with NULL-in-any-FK-column skip (probe-confirmed for partial-NULL composites too) and raises Msg 547 with probe-confirmed wording — "FOREIGN KEY constraint" / "FOREIGN KEY SAME TABLE" for self-refs / column phrase dropped for composites; EnforceIncomingForeignKeys (DELETE) and EnforceIncomingFkOnUpdate (UPDATE, threading parent old/new pairs so ON UPDATE CASCADE rewrites land correctly) raise Msg 547 with "REFERENCE constraint" wording on NO_ACTION or recursively descend for CASCADE / SET NULL / SET DEFAULT, capped at MaxCascadeDepth = 32. CREATE-time validation: referenced columns must form a PK/UNIQUE (Msg 1776, multiset compare against KeyConstraints); cascade-cycle / multiple-path graph walk over the existing FK graph plus already-resolved-in-this-statement FKs raises Msg 1785, with the new FK excluded from its own reachability check and self-reference + cascading action short-circuited as the explicit 1-cycle. DROP TABLE on a referenced parent → Msg 3726; successful child DROP detaches outgoing FKs from each parent's incoming list. Auto-name follows the project's FNV-1a convention: FK__<table>__<col>__<hex> single / FK__<table>__<hex> composite. Catalog: sys.objects interleaves 'F ' / FOREIGN_KEY_CONSTRAINT rows after each child table; new sys.foreign_keys (22 cols matching the probed shape, action codes + descriptions + is_system_named / is_disabled / is_not_trusted) + sys.foreign_key_columns (6 cols, one row per pair). Grammar plumbing limited to a new Action contextual keyword (everything else reuses existing reserved tokens). EF Core 10's HasOne / WithMany / HasForeignKey / OnDelete(DeleteBehavior.Cascade) end-to-end through SaveChanges. New ForeignKeyTests covering inline / table-level / composite / named / self-ref / NULL-skip / all four actions on DELETE+UPDATE / cascade chains / MERGE / multi-row rollback / Msg 1776 + 1785 + 3726 + 547 wording variants / catalog projection, plus tx-rollback tests confirming explicit ROLLBACK restores the entire CASCADE / SET NULL / SET DEFAULT / ON UPDATE CASCADE chain, SAVE TRAN + ROLLBACK TRAN <name> rewinds the cascade while keeping the outer tx alive, and statement-atomic rollback restores every row when a multi-hop chain hits a NO ACTION leaf (works without code changes — every cascade mutation threads through the active UndoLog); new EFCoreForeignKey validates the LINQ-side relationship round-trip. New SqlServerSimulator.Tests/CLAUDE.md documents the dense multi-statement-raw-string test shape so future tests land dense first time. CLAUDE.md "Not modeled" FK bullet removed; new Constraints entry + trigger phrase pointing at the new docs/claude/foreign-keys.md covering grammar / storage / validation / enforcement / catalog / EF Core / fidelity gaps (ALTER TABLE ADD/DROP CONSTRAINT deferred; OBJECT_ID(name, 'F') returns NULL; key_index_id always 1; SET DEFAULT-to-NULL on NOT NULL surfaces at runtime as Msg 515 not CREATE-time Msg 1789).1 parent a727a2c commit 8ea005a
18 files changed
Lines changed: 2414 additions & 24 deletions
File tree
- SqlServerSimulator.Tests.EFCore
- SqlServerSimulator.Tests
- SqlServerSimulator
- Errors
- Parser
- Simulation
- Storage
- docs/claude
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
65 | | - | |
| 65 | + | |
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
| |||
105 | 105 | | |
106 | 106 | | |
107 | 107 | | |
| 108 | + | |
108 | 109 | | |
109 | 110 | | |
110 | 111 | | |
| |||
149 | 150 | | |
150 | 151 | | |
151 | 152 | | |
| 153 | + | |
152 | 154 | | |
153 | 155 | | |
154 | 156 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
0 commit comments