From c3572a5b56c1e70e2572081e00a65d413dd87dc3 Mon Sep 17 00:00:00 2001 From: hypercross Date: Sun, 19 Apr 2026 23:57:47 +0800 Subject: [PATCH] fix(csv-loader): handle object foreign keys in reverse lookups Update `csvToModule` to check if a foreign key value is an object containing the default primary key before converting it to a string. This ensures correct key mapping when references are resolved as objects rather than primitive IDs. --- src/csv-loader/loader.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/csv-loader/loader.ts b/src/csv-loader/loader.ts index a2fe4e2..1c213d2 100644 --- a/src/csv-loader/loader.ts +++ b/src/csv-loader/loader.ts @@ -1199,7 +1199,8 @@ export function csvToModule( reverseLookupInits.push( `const ${revLookupVar} = new Map();`, `for (const r of _raw) {`, - ` const k = String(r.${decl.foreignKey});`, + ` const kv = r.${decl.foreignKey};`, + ` const k = String(typeof kv === "object" && "${defaultPrimaryKey}" in kv ? kv.${defaultPrimaryKey} : kv);`, ` if (!${revLookupVar}.has(k)) ${revLookupVar}.set(k, []);`, ` ${revLookupVar}.get(k).push(r);`, `}`, @@ -1209,7 +1210,8 @@ export function csvToModule( reverseLookupInits.push( `const ${revLookupVar} = new Map();`, `for (const r of ${varName}()) {`, - ` const k = String(r.${decl.foreignKey});`, + ` const kv = r.${decl.foreignKey};`, + ` const k = String(typeof kv === "object" && "${defaultPrimaryKey}" in kv ? kv.${defaultPrimaryKey} : kv);`, ` if (!${revLookupVar}.has(k)) ${revLookupVar}.set(k, []);`, ` ${revLookupVar}.get(k).push(r);`, `}`,