Error:Cannot build query because main alias is not set
See original GitHub issueIssue type:
[x] question [ ] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[x] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)
Steps to reproduce or a small repository showing the problem:
I want to create this query:
SELECT
sum( num )
FROM
( SELECT count( DISTINCT `ele_red_user_record`.`user_id` ) AS `num` FROM `ele_red_user_record` GROUP BY `ele_red_user_record`.`date` ORDER BY `ele_red_user_record`.`date` DESC ) s
I wrote this code:
const sumQuery = repository
.createQueryBuilder()
.select('sum(num)', 'total_sum')
.from(subQuery => {
return subQuery
.select('count(distinct ele_red_user_record.user_id)', 'num')
.groupBy('ele_red_user_record.date')
.orderBy('ele_red_user_record.date', 'DESC');
}, 's');
But, I got this
Error: Cannot build query because main alias is not set (call qb#from method)
at SelectQueryBuilder.createSelectExpression (node_modules/_typeorm@0.2.19@typeorm/query-builder/SelectQueryBuilder.js:950:19)
at SelectQueryBuilder.getQuery (node_modules/_typeorm@0.2.19@typeorm/query-builder/SelectQueryBuilder.js:42:24)
at SelectQueryBuilder.QueryBuilder.createFromAlias (node_modules/_typeorm@0.2.19@typeorm/query-builder/QueryBuilder.js:363:44)
at SelectQueryBuilder.from (node_modules/_typeorm@0.2.19@typeorm/query-builder/SelectQueryBuilder.js:119:30)
at Object.getTotalReceiveUsersCount (EleRedUserRecord.mjs:21:8)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:9
Top Results From Across the Web
Typeorm Subquery - Stack Overflow
I've tried a couple of ways to write it in QueryBuilder, but keep getting an error Cannot build query because main alias is...
Read more >Error: Main alias is not set - typeorm/typeorm - Gitter
I've a new issue: when I want to run a QueryBuilder to build an update statement, ... TypeError: Cannot read property 'hasMetadata' of...
Read more >TypeORM - Query Builder with Subquery - DEV Community
This is the main part to demonstrate how we should build complex queries with TypeORM.
Read more >Select using Query Builder - typeorm - GitBook
SelectQueryBuilder - used to build and execute SELECT queries. Example: ... One query builder is not limited to one alias, they can have...
Read more >SelectQueryBuilder | typeorm
Allows to build complex sql queries in a fashion way and execute those queries. ... Gets the main alias string used in this...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
typeOrm is garbage just so yall know
On postgres I was able to workaround the error using the other method for adding subqueries to a
fromclause suggested in https://typeorm.io/#/select-query-builder/using-subqueries (just before it recommends the “more elegant syntax” that leads to this issue).getQueryand.getParamsThe example from the docs is as follows:
But it’s disappointing that the “more elegant syntax” suggested by the docs doesn’t work.