{"record":{"id":"6231f175649ab76b","repo":"payloadcms/payload","slug":"could-not-retrieve-sanitized-polymorphic-joins-con","errorCode":null,"errorMessage":"Could not retrieve sanitized polymorphic joins config for ${collection}.","messagePattern":"Could not retrieve sanitized polymorphic joins config for (.+?)\\.","errorType":"http","errorClass":"APIError","httpStatus":500,"severity":"error","filePath":"packages/db-mongodb/src/utilities/buildJoinAggregation.ts","lineNumber":68,"sourceCode":"  if (\n    (Object.keys(collectionConfig.joins).length === 0 &&\n      collectionConfig.polymorphicJoins.length == 0) ||\n    joins === false\n  ) {\n    return []\n  }\n\n  const joinConfig = adapter.payload.collections[collection]?.config?.joins\n\n  if (!joinConfig) {\n    throw new APIError(`Could not retrieve sanitized join config for ${collection}.`)\n  }\n\n  const aggregate: PipelineStage[] = []\n  const polymorphicJoinsConfig = adapter.payload.collections[collection]?.config?.polymorphicJoins\n\n  if (!polymorphicJoinsConfig) {\n    throw new APIError(`Could not retrieve sanitized polymorphic joins config for ${collection}.`)\n  }\n\n  for (const join of polymorphicJoinsConfig) {\n    if (projection && !projection[join.joinPath]) {\n      continue\n    }\n\n    if (joins?.[join.joinPath] === false) {\n      continue\n    }\n\n    const {\n      count = false,\n      limit: limitJoin = join.field.defaultLimit ?? 10,\n      page,\n      sort: sortJoin = join.field.defaultSort || collectionConfig.defaultSort,\n      where: whereJoin = {},\n    } = joins?.[join.joinPath] || {}","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/payloadcms/payload/blob/00c58b35c0ed348ddc22daabf467b139727214fd/packages/db-mongodb/src/utilities/buildJoinAggregation.ts#L50-L86","documentation":"Companion to the join-config error: thrown when `adapter.payload.collections[collection]?.config?.polymorphicJoins` is falsy even though the raw config indicated polymorphic joins exist. Indicates incomplete sanitization or a slug mismatch for the target collection.","triggerScenarios":"A query that triggers polymorphic join aggregation where the target collection's sanitized config lacks `polymorphicJoins` — slug mismatch, sanitization dropped the entry, or query during init.","commonSituations":"Renamed collection; polymorphic join target collection not loaded (e.g. plugin providing it not registered); sanitization failure on the join target; init race.","solutions":["Verify the target collection slug is registered and sanitized.","Confirm all collections referenced by polymorphic joins are loaded.","Await init; restart to rebuild sanitized configs.","Check plugin load order if joins cross plugin boundaries."],"exampleFix":"// before\npolymorphic join references 'media' but plugin not registered\n// after\npayload.init({ collections: [...coreCollections, ...mediaPluginCollections] })","handlingStrategy":"validation","validationCode":"function assertPolymorphicJoins(payload, collection) {\n  const cfg = payload.collections[collection]?.config?.polymorphicJoins\n  if (!cfg) throw new Error(`No sanitized polymorphic joins for ${collection}`)\n  return cfg\n}","typeGuard":"const hasSanitizedPolymorphicJoins = (payload, c) =>\n  Boolean(payload.collections[c]?.config?.polymorphicJoins)","tryCatchPattern":"try { await payload.find({ collection, joins: {...} }) }\ncatch (e) { if (/sanitized polymorphic joins/.test(e.message)) handleBadPolyJoin(c) else throw e }","preventionTips":["Ensure all collections referenced by polymorphic joins are loaded.","Check plugin load order for cross-plugin joins.","Await init; restart to rebuild sanitized configs."],"tags":["mongodb","joins","polymorphic","config","sanitization"],"backgroundTag":null,"analyzedSha":"00c58b35c0ed348ddc22daabf467b139727214fd","analyzedAt":"2026-08-12T20:45:03.758Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}