{"record":{"id":"e7b7673f573e5572","repo":"n8n-io/n8n","slug":"entity-to-work-with-is-not-specified","errorCode":null,"errorMessage":"Entity to work with is not specified!","messagePattern":"Entity to work with is not specified!","errorType":"exception","errorClass":"TypeORMError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/typeorm/src/query-builder/QueryExpressionMap.ts","lineNumber":454,"sourceCode":"\t\tif (!alias)\n\t\t\tthrow new TypeORMError(`\"${aliasName}\" alias was not found. Maybe you forgot to join it?`);\n\n\t\treturn alias;\n\t}\n\n\tfindColumnByAliasExpression(aliasExpression: string): ColumnMetadata | undefined {\n\t\tconst [aliasName, propertyPath] = aliasExpression.split('.');\n\t\tconst alias = this.findAliasByName(aliasName);\n\t\treturn alias.metadata.findColumnWithPropertyName(propertyPath);\n\t}\n\n\t/**\n\t * Gets relation metadata of the relation this query builder works with.\n\t *\n\t * todo: add proper exceptions\n\t */\n\tget relationMetadata(): RelationMetadata {\n\t\tif (!this.mainAlias) throw new TypeORMError(`Entity to work with is not specified!`); // todo: better message\n\n\t\tconst relationMetadata = this.mainAlias.metadata.findRelationWithPropertyPath(\n\t\t\tthis.relationPropertyPath,\n\t\t);\n\t\tif (!relationMetadata)\n\t\t\tthrow new TypeORMError(\n\t\t\t\t`Relation ${this.relationPropertyPath} was not found in entity ${this.mainAlias.name}`,\n\t\t\t); // todo: better message\n\n\t\treturn relationMetadata;\n\t}\n\n\t/**\n\t * Copies all properties of the current QueryExpressionMap into a new one.\n\t * Useful when QueryBuilder needs to create a copy of itself.\n\t */\n\tclone(): QueryExpressionMap {\n\t\tconst map = new QueryExpressionMap(this.connection);","sourceCodeStart":436,"sourceCodeEnd":472,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/typeorm/src/query-builder/QueryExpressionMap.ts#L436-L472","documentation":"The relationMetadata getter on QueryExpressionMap requires a mainAlias (the entity the builder was created from). It throws TypeORMError when mainAlias is falsy, which happens when createQueryBuilder() was called without a target entity (e.g. raw query builder) but a relation API (.relation(...), .of(...), .set/.add/.remove) was then used.","triggerScenarios":"Calling dataSource.createQueryBuilder().relation('User.projects') without first binding an entity via .from(User) or createQueryBuilder(User). Using a relation builder off a raw SQL builder returned by createQueryBuilder<SomeShape>().","commonSituations":"Migrating a raw query to the relation API; helper functions that accept a generic SelectQueryBuilder and accidentally invoke relation operations; chaining .relation() on a builder obtained from manager.createQueryBuilder() with no generic.","solutions":["Construct the relation builder from an entity-bound builder: dataSource.createQueryBuilder(User).relation('projects').of(user).add(project).","If you only need raw SQL, drop the .relation() call and use .insert().into(...)/.update() directly.","Ensure the helper that hands out the builder always seeds it with an entity target."],"exampleFix":"// before\nawait dataSource.createQueryBuilder().relation('User.projects').of(user).add(p);\n// after\nawait dataSource.createQueryBuilder(User).relation('projects').of(user).add(p);","handlingStrategy":"type-guard","validationCode":"import { SelectQueryBuilder } from 'typeorm';\n\nfunction ensureMainAlias<T>(qb: SelectQueryBuilder<T>): asserts qb is SelectQueryBuilder<T> {\n  if (!qb.expressionMap.mainAlias)\n    throw new Error('Builder has no main entity alias; create it via createQueryBuilder(Entity).');\n}","typeGuard":"function hasMainAlias(qb: SelectQueryBuilder<any>): boolean {\n  return !!qb.expressionMap.mainAlias;\n}","tryCatchPattern":null,"preventionTips":["Always seed createQueryBuilder with an entity target when relation APIs will be used.","Wrap relation-builder factories in helpers that take an entity class, not a generic builder."],"tags":["typeorm","query-builder","relation","main-alias"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}