{"record":{"id":"41e86396a0d462a9","repo":"n8n-io/n8n","slug":"main-alias-is-not-set","errorCode":null,"errorMessage":"Main alias is not set","messagePattern":"Main alias is not set","errorType":"exception","errorClass":"TypeORMError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/typeorm/src/query-builder/QueryBuilder.ts","lineNumber":137,"sourceCode":"\n\t// -------------------------------------------------------------------------\n\t// Abstract Methods\n\t// -------------------------------------------------------------------------\n\n\t/**\n\t * Gets generated SQL query without parameters being replaced.\n\t */\n\tabstract getQuery(): string;\n\n\t// -------------------------------------------------------------------------\n\t// Accessors\n\t// -------------------------------------------------------------------------\n\n\t/**\n\t * Gets the main alias string used in this query builder.\n\t */\n\tget alias(): string {\n\t\tif (!this.expressionMap.mainAlias) throw new TypeORMError(`Main alias is not set`); // todo: better exception\n\n\t\treturn this.expressionMap.mainAlias.name;\n\t}\n\n\t// -------------------------------------------------------------------------\n\t// Public Methods\n\t// -------------------------------------------------------------------------\n\n\t/**\n\t * Creates SELECT query.\n\t * Replaces all previous selections if they exist.\n\t */\n\tselect(): SelectQueryBuilder<Entity>;\n\n\t/**\n\t * Creates SELECT query and selects given data.\n\t * Replaces all previous selections if they exist.\n\t */","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/typeorm/src/query-builder/QueryBuilder.ts#L119-L155","documentation":"QueryBuilder.alias getter throws when expressionMap.mainAlias is null. The main alias is set by select()/from()/into(); before any of those calls, there is no main alias and any method that reads .alias (e.g. escape resolution, getMainTableName, alias-dependent where clauses) fails. This is a developer-error guard, not a runtime recovery point.","triggerScenarios":"Calling createQueryBuilder() with no entity and then immediately invoking a method that reads this.alias (e.g. .where('alias.col = :v'), .orderBy, .getSql) before select/from/into. Also when chaining .delete()/.update() without a FROM alias on some drivers.","commonSituations":"Creating a blank QueryBuilder and forgetting to anchor it to an entity or table. Refactoring that drops the .from() call. Conditional builder construction that skips the anchor in a branch.","solutions":["Anchor the builder: createQueryBuilder(Entity, 'alias') or call .from(Entity, 'alias') / .into(Entity) / .select() first.","For delete/update without a FROM entity, use dataSource.createQueryBuilder().delete().from(Entity).","Ensure any conditional branch that returns a QueryBuilder always sets a main alias."],"exampleFix":"// before\nconst qb = dataSource.createQueryBuilder();\nqb.where('u.id = :id', { id }); // throws: no main alias\n\n// after\nconst qb = dataSource.createQueryBuilder(User, 'u').where('u.id = :id', { id });","handlingStrategy":"validation","validationCode":"function builderHasMainAlias(qb: { expressionMap: { mainAlias?: unknown } }): boolean {\n  return qb.expressionMap.mainAlias != null;\n}\nif (!builderHasMainAlias(qb)) throw new Error('QueryBuilder has no main alias; call select/from/into first');","typeGuard":"function hasMainAlias(qb: { expressionMap: { mainAlias?: unknown } }): qb is { expressionMap: { mainAlias: NonNullable<unknown> } } {\n  return qb.expressionMap.mainAlias != null;\n}","tryCatchPattern":null,"preventionTips":["Anchor every QueryBuilder with an entity and alias at construction.","Use createQueryBuilder(Entity, 'alias') rather than the no-arg form.","Ensure conditional builder construction always sets a main alias.","Add a helper that asserts a main alias before returning a builder."],"tags":["query-builder","alias","setup"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}