{"record":{"id":"f194b632f76aa090","repo":"n8n-io/n8n","slug":"could-not-find-any-entity-of-type-target-matc","errorCode":null,"errorMessage":"Could not find any entity of type \"${target}\" matching: ${criteria}","messagePattern":"Could not find any entity of type \"(.+?)\" matching: (.+?)","errorType":"exception","errorClass":"EntityNotFoundError","httpStatus":404,"severity":"error","filePath":"packages/@n8n/typeorm/src/query-builder/SelectQueryBuilder.ts","lineNumber":1593,"sourceCode":"\t\t\t\t\t\tactualVersion,\n\t\t\t\t\t);\n\t\t\t}\n\t\t}\n\n\t\tif (result === undefined) {\n\t\t\treturn null;\n\t\t}\n\t\treturn result;\n\t}\n\n\t/**\n\t * Gets the first entity returned by execution of generated query builder sql or rejects the returned promise on error.\n\t */\n\tasync getOneOrFail(): Promise<Entity> {\n\t\tconst entity = await this.getOne();\n\n\t\tif (!entity) {\n\t\t\tthrow new EntityNotFoundError(\n\t\t\t\tthis.expressionMap.mainAlias!.target,\n\t\t\t\tthis.expressionMap.parameters,\n\t\t\t);\n\t\t}\n\n\t\treturn entity;\n\t}\n\n\t/**\n\t * Gets entities returned by execution of generated query builder sql.\n\t */\n\tasync getMany(): Promise<Entity[]> {\n\t\tif (this.expressionMap.lockMode === 'optimistic') throw new OptimisticLockCanNotBeUsedError();\n\n\t\tconst results = await this.getRawAndEntities();\n\t\treturn results.entities;\n\t}\n","sourceCodeStart":1575,"sourceCodeEnd":1611,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/typeorm/src/query-builder/SelectQueryBuilder.ts#L1575-L1611","documentation":"Thrown as EntityNotFoundError from getOneOrFail() when the underlying getOne() returns null. It reports the main alias target and the bound parameters so the caller can see what was searched for. Unlike getOne() (which resolves null), this variant rejects, enforcing that an entity must exist.","triggerScenarios":"Calling `.getOneOrFail()` on a query whose WHERE matches zero rows: wrong id, soft-deleted row excluded by a global scope, filtered tenant, or a race where the row was deleted between select-for-update and getOne.","commonSituations":"Lookup-by-id endpoints that should 404; reference-data loads where a foreign key points at a missing row; tests against a freshly-migrated DB missing seed data; multi-tenant filters that silently exclude the row.","solutions":["If a missing row is a legitimate case, use getOne() and handle null instead of getOneOrFail().","If the row must exist, verify the id/tenant/filter before querying, and return a 404 with context to the caller.","Check scopes/soft-delete/global filters that may be hiding the row."],"exampleFix":"// before\nconst user = await repo.createQueryBuilder().where({ id }).getOneOrFail();\n// after\nconst user = await repo.createQueryBuilder().where({ id }).getOne();\nif (!user) throw new NotFoundException(`user ${id} not found`);","handlingStrategy":"validation","validationCode":"async function findOr404<T>(qb: SelectQueryBuilder<T>): Promise<T> {\n  const entity = await qb.getOne();\n  if (!entity) {\n    const err = new Error(`entity not found`);\n    (err as any).status = 404;\n    throw err;\n  }\n  return entity;\n}\n// then: const u = await findOr404(repo.createQueryBuilder().where({ id }));","typeGuard":"function isEntityNotFoundError(e: unknown): boolean {\n  return e instanceof Error && e.name === 'EntityNotFoundError';\n}","tryCatchPattern":"try {\n  return await qb.getOneOrFail();\n} catch (e) {\n  if (e?.name === 'EntityNotFoundError') throw new NotFoundException('resource not found');\n  throw e;\n}","preventionTips":["Prefer getOne() with explicit null checks when a missing row is a valid case.","Map EntityNotFoundError to an HTTP 404 at the controller boundary.","Check soft-delete/global filters and tenant scopes that may hide the row."],"tags":["typeorm","query-builder","not-found","validation"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}