payloadcms/payload · error · APIError

Collection with the slug ${collectionSlug} was not found.

Error message

Collection with the slug ${collectionSlug} was not found.

What it means

During nested relationship query building, buildSearchParam iterates the reversed path list and throws an APIError when a path entry has no collectionSlug. Note the message is misleading: it fires precisely when collectionSlug is undefined (so the message reads 'Collection with the slug undefined was not found'), not when a slug is unknown.

Source

Thrown at packages/db-mongodb/src/queries/buildSearchParams.ts:138

    if (!formattedOperator) {
      return undefined
    }

    // If there are multiple collections to search through,
    // Recursively build up a list of query constraints
    if (paths.length > 1) {
      // Remove top collection and reverse array
      // to work backwards from top
      const pathsToQuery = paths.slice(1).reverse()

      let relationshipQuery: SearchParam = {
        value: {},
      }

      for (const [i, { collectionSlug, path: subPath }] of pathsToQuery.entries()) {
        if (!collectionSlug) {
          throw new APIError(`Collection with the slug ${collectionSlug} was not found.`)
        }

        const { collectionConfig, Model: SubModel } = getCollection({
          adapter: payload.db as MongooseAdapter,
          collectionSlug,
        })

        if (i === 0) {
          const subQuery = await SubModel.buildQuery({
            locale,
            payload,
            where: {
              [subPath]: {
                [formattedOperator]: val,
              },
            },
          })

View on GitHub (pinned to 00c58b35c0)

Solutions

  1. Simplify the query path and confirm each segment is a relationship/upload/join with a resolvable target collection.
  2. Inspect the field config for the path; ensure relationTo is set and the target collection exists.
  3. Avoid querying through fields that are not relationships (arrays/blocks/groups do not carry a collectionSlug hop).
  4. Check payload.config for the queried collection/field to confirm the relationship chain is intact.

Example fix

// before — querying through a non-relationship segment
await payload.find({
  collection: 'orders',
  where: { 'customer.address.someField': { equals: 'x' } }, // address is a group, not a relation
})

// after — query only through relationship segments
await payload.find({
  collection: 'orders',
  where: { 'customer.name': { equals: 'x' } }, // customer is a relationship
})
Defensive patterns

Strategy: validation

Validate before calling

// Validate each segment of a nested query path is a relationship before querying
import { getFieldByPath } from 'payload'

function assertPathIsRelational(fields: any[], path: string) {
  const segments = path.split('.')
  let currentFields = fields
  for (const seg of segments.slice(0, -1)) {
    const f = getFieldByPath({ fields: currentFields, path: seg })
    if (!f || (f.type !== 'relationship' && f.type !== 'upload' && f.type !== 'join')) {
      throw new Error(`Path segment '${seg}' is not relational — cannot nest query through it`)
    }
  }
}

Type guard

function isRelationalField(f: unknown): f is { type: 'relationship' | 'upload' | 'join' } {
  return typeof f === 'object' && f !== null &&
    ['relationship', 'upload', 'join'].includes((f as any).type)
}

Try / catch

try {
  await payload.find({ collection: 'orders', where: { 'customer.name': { equals: 'x' } } })
} catch (e) {
  if (e instanceof APIError && /Collection with the slug .* was not found/.test(e.message)) {
    console.error('Query path traverses a non-relational segment — simplify the where clause')
  }
  throw e
}

Prevention

When it happens

Trigger: A multi-hop where/relationship query (path traverses >1 collection) where one hop's PathToQuery entry lacks a collectionSlug — e.g. a relationship/join field whose relationTo could not be resolved, or a malformed deeply-nested query path.

Common situations: Querying a nested relationship path where an intermediate field is not a relationship (so no target collection); config drift where a relationship field was changed to another type; a join field whose `on` references a non-relationship field.

Related errors


AI-assisted analysis of payloadcms/payload@00c58b35c0 (2026-08-12). Data as JSON: /api/errors/02514db28e03f5ba. Report an issue: GitHub.