|
| 1 | +name: roundtrip-self-fk |
| 2 | +description: | |
| 3 | + 1:N self-referential FK write round-trip. Node has id (PK) + a NULLABLE |
| 4 | + parentId → Node.id (declared via `identity.reference fkParent → Node`). Two |
| 5 | + roundtrips: a root node (parentId omitted → NULL) and a child whose parentId |
| 6 | + points at a seeded parent row. Each INSERTs through the port's runtime / ORM |
| 7 | + write codec (NOT raw SQL), reads the row back by primary key, and asserts the |
| 8 | + wire-normalized read-back (the PK is excluded from the comparison per the |
| 9 | + op:roundtrip contract). |
| 10 | +
|
| 11 | + This is the ONLY 1:N self-referential FK in the corpus — every other self- |
| 12 | + reference is an M:N junction self-join (FR-018 @symmetric / @sourceRefField). |
| 13 | + A 1:N self-FK is realized as a plain scalar column (field.long) whose FK-ness |
| 14 | + is declared by identity.reference; every port emits it as a scalar (TS/Drizzle |
| 15 | + alone emits a column-level `.references()`, which is why only TS could hit a |
| 16 | + self-import bug). The round-trip gates that the write codec handles a nullable |
| 17 | + self-FK uniformly: NULL stays NULL, and a non-null value satisfies the self- |
| 18 | + referencing FK constraint. |
| 19 | +
|
| 20 | + Wire rule: parentId is field.long → BIGINT → a numeric STRING on the wire |
| 21 | + ("1000"); a NULL FK stays JSON null. |
| 22 | +
|
| 23 | +# Seed a parent row so the child roundtrip's parentId references a real id. |
| 24 | +# id 1000 is well above the IDENTITY sequence start, so the runtime auto-id |
| 25 | +# inserts below it never collide (the column is GENERATED BY DEFAULT, so the |
| 26 | +# explicit seed value does not advance the sequence). |
| 27 | +seed-data: | |
| 28 | + INSERT INTO "nodes" ("id", "label", "parentId") VALUES (1000, 'root', NULL); |
| 29 | +
|
| 30 | +queries: |
| 31 | + - name: write-root-node-null-parent |
| 32 | + op: roundtrip |
| 33 | + entity: Node |
| 34 | + insert: |
| 35 | + label: "another root" |
| 36 | + # parentId omitted → NULL self-FK |
| 37 | + expect: |
| 38 | + label: "another root" |
| 39 | + parentId: null |
| 40 | + |
| 41 | + - name: write-child-node-with-parent |
| 42 | + op: roundtrip |
| 43 | + entity: Node |
| 44 | + insert: |
| 45 | + label: "child" |
| 46 | + parentId: 1000 |
| 47 | + expect: |
| 48 | + label: "child" |
| 49 | + parentId: "1000" |
0 commit comments