Skip to content

Commit 528c955

Browse files
os-zhuangclaude
andcommitted
fix(objectql): seed reference resolution falls back to matching by id
SeedLoaderService.resolveFromDatabase only matched a reference value against the target's natural-key field. A seed that wires a lookup to a REAL existing record by its internal id — e.g. a people field (approver/applicant → user) pointed at the current user — dangled to null when that id is not a UUID/ObjectId (so the caller's `looksLikeInternalId` guard did not short-circuit) and is not the target's natural key. Add an id fallback: when the natural-key lookup finds nothing, try resolving the value as the target's `id`. Safe — an id either exists or it doesn't, so there's no risk of a false natural-key match; and it's tenant-scoped like the primary lookup. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 593d43b commit 528c955

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

packages/objectql/src/seed-loader.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,25 @@ export class SeedLoaderService implements ISeedLoaderService {
448448
if (records && records.length > 0) {
449449
return String(records[0].id || records[0]._id);
450450
}
451+
// Fallback: the value may already be the target's internal id rather than
452+
// its natural key — a seed that wires a lookup to a real existing record
453+
// (e.g. a people field → the current user, whose id is not a UUID/ObjectId
454+
// so `looksLikeInternalId` did not short-circuit). Resolving by id lets a
455+
// valid id resolve instead of dangling null, with no risk of a false
456+
// natural-key match (an id either exists or it does not).
457+
if (targetField !== 'id') {
458+
const byId: Record<string, unknown> = { id: value };
459+
if (organizationId) byId.organization_id = organizationId;
460+
const idMatch = await this.engine.find(targetObject, {
461+
where: byId,
462+
fields: ['id'],
463+
limit: 1,
464+
context: { isSystem: true },
465+
} as any);
466+
if (idMatch && idMatch.length > 0) {
467+
return String(idMatch[0].id || idMatch[0]._id);
468+
}
469+
}
451470
} catch {
452471
// Target object may not exist yet
453472
}

0 commit comments

Comments
 (0)