{"record":{"id":"03b3a547f9993d52","repo":"payloadcms/payload","slug":"relationship-was-not-found","errorCode":null,"errorMessage":"Relationship was not found","messagePattern":"Relationship was not found","errorType":"http","errorClass":"APIError","httpStatus":500,"severity":"error","filePath":"packages/drizzle/src/queries/getTableColumnFromPath.ts","lineNumber":392,"sourceCode":"        if (field.hasMany) {\n          const relationTableName = `${adapter.tableNameMap.get(toSnakeCase(field.collection))}${adapter.relationshipsSuffix}`\n\n          const existingTable = joins.find(\n            (e) => e.queryPath === `${constraintPath}${field.name}._rels`,\n          )\n\n          const aliasRelationshipTable = (existingTable?.table ??\n            getTableAlias({\n              adapter,\n              tableName: relationTableName,\n            }).newAliasTable) as PgTableWithColumns<any>\n\n          const relationshipField = getFieldByPath({\n            fields: adapter.payload.collections[field.collection].config.flattenedFields,\n            path: field.on,\n          })\n          if (!relationshipField) {\n            throw new APIError('Relationship was not found')\n          }\n\n          if (!existingTable) {\n            addJoinTable({\n              condition: and(\n                eq(\n                  adapter.tables[rootTableName].id,\n                  aliasRelationshipTable[\n                    `${(relationshipField.field as RelationshipField).relationTo as string}ID`\n                  ],\n                ),\n                like(aliasRelationshipTable.path, field.on),\n              ),\n              joins,\n              queryPath: `${constraintPath}${field.name}._rels`,\n              table: aliasRelationshipTable,\n            })\n          }","sourceCodeStart":374,"sourceCodeEnd":410,"githubUrl":"https://github.com/payloadcms/payload/blob/00c58b35c0ed348ddc22daabf467b139727214fd/packages/drizzle/src/queries/getTableColumnFromPath.ts#L374-L410","documentation":"`APIError('Relationship was not found')` from the hasMany branch of `getTableColumnFromPath`: when traversing a hasMany relationship through the relationships/join table, the adapter looks up the target field named by the relationship's `on` property in the target collection's `flattenedFields`. If that lookup fails (no field at that path on the target collection), the join condition cannot be built and the query aborts.","triggerScenarios":"Querying through a hasMany relationship whose `on` field refers to a field that doesn't exist on the related collection's config — e.g. after renaming/deleting the related field, or a stale `on` value, or a relationship whose target collection is misconfigured.","commonSituations":"Renaming the relationship target field without updating the relationship's `on`; relationship config pointing at a field added by a plugin that isn't loaded; copy-paste relationship config between collections with different field names.","solutions":["Verify the relationship field's `on` value names an actual field on the target collection (check `flattenedFields`).","If the target field was renamed, update the relationship's `on` to the new name and regenerate migrations.","Ensure any plugin contributing the target field is loaded in the config running the query."],"exampleFix":"// before: relationship 'on' references a field that no longer exists\n//   { name: 'items', type: 'relationship', relationTo: 'items', hasMany: true, on: 'sectionID' }\n//   but items collection renamed sectionID -> section_id\n// after\n//   { ..., on: 'section_id' }","handlingStrategy":"validation","validationCode":"import { getFieldByPath } from 'payload'\nconst rel = payload.collections[collection].config.flattenedFields[seg]\nif (rel?.hasMany) {\n  const target = payload.collections[rel.relationTo].config\n  if (!getFieldByPath({ fields: target.flattenedFields, path: rel.on })) {\n    throw new Error(`Relationship '${seg}' on='${rel.on}' not found on '${rel.relationTo}'`)\n  }\n}","typeGuard":"const relationshipTargetFieldExists = (rel, payload) =>\n  payload.collections[rel.relationTo]?.config.flattenedFields.some(f => f.name === rel.on)","tryCatchPattern":"try {\n  await payload.find({ collection, where })\n} catch (err) {\n  if (err?.message === 'Relationship was not found') {\n    payload.logger.error(`A hasMany relationship's 'on' field is missing on its target collection.`)\n  }\n  throw err\n}","preventionTips":["Keep relationship 'on' fields in sync when renaming target fields.","Load all plugins contributing relationship target fields before running queries.","Validate relationship config at boot against the resolved collection fields."],"tags":["query","relationships","config-lookup","hasmany"],"backgroundTag":null,"analyzedSha":"00c58b35c0ed348ddc22daabf467b139727214fd","analyzedAt":"2026-08-12T20:45:03.758Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}