Fix import FK fault: derived/back-linked litter referenced a not-yet-inserted parent
BUG (blocked the supervised live re-import): /import/execute threw Npgsql 23503 FK_Litters_Gerbils_FatherId and rolled back. Root cause was INSERT ORDERING — synthesized derived litters were SaveChanges()'d BEFORE the parent gerbils (created later in the animal loop), so the litter's Father/MotherId pointed at rows that didn't exist yet. The dry-run and the EF in-memory test provider don't enforce FKs, so it slipped through. Fix: - Stage synthesized litters in the context but DON'T save them early; the single SaveChanges after the animal loop lets EF order parents → litters → offspring (all FKs nullable). Saving litters first was the fault. - FK-integrity guard (god's spec): compute the persisted set (existing DB + this run's loadable) and null out any litter parent FK not in it; SKIP a derived litter whose BOTH parents are unresolvable (offspring loads with LitterId=null). Quarantined parents already resolve to null via ResolveParentGid; this is defense-in-depth + makes the invariant explicit. - Report litterParentFksDropped + derivedLittersSkipped (LitterSummary) + a German note — so a green dry-run (0/0) GUARANTEES execute won't FK-fault. Tests: two SQLite-backed regressions (SQLite enforces FKs, unlike the in-memory provider) — a derived litter with NEW chart parents executes without throwing, and a quarantined parent leaves that FK null. 120 C# tests + python green; no schema change (has-pending clean). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -81,7 +81,8 @@ namespace GerbilManagerWebAPI.Import
|
||||
/// Zuchtname animals made resident because they parented a Clan offspring (rule b).</summary>
|
||||
public sealed record ResidencySummary(int Resident, int External, int FlippedByParentRule);
|
||||
|
||||
public sealed record LitterSummary(int InSource, int Created, int AlreadyImported, int DerivedFromChart = 0);
|
||||
public sealed record LitterSummary(int InSource, int Created, int AlreadyImported,
|
||||
int DerivedFromChart = 0, int DerivedSkipped = 0, int ParentFksDropped = 0);
|
||||
|
||||
public sealed record AnimalSummary(
|
||||
int InSource,
|
||||
|
||||
Reference in New Issue
Block a user