Error:Cannot build query because main alias is not set

See original GitHub issue

Issue 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:open
  • Created 4 years ago
  • Reactions:4
  • Comments:9

github_iconTop GitHub Comments

28reactions
OmarLomsadzecommented, May 5, 2022

typeOrm is garbage just so yall know

4reactions
brahncommented, Dec 24, 2020

On postgres I was able to workaround the error using the other method for adding subqueries to a from clause suggested in https://typeorm.io/#/select-query-builder/using-subqueries (just before it recommends the “more elegant syntax” that leads to this issue)

  1. Create an entirely new querybuilder for the subquery
  2. Jam it in using .getQuery and .getParams

The example from the docs is as follows:

const userQb = await connection.getRepository(User)
    .createQueryBuilder("user")
    .select("user.name", "name")
    .where("user.registered = :registered", { registered: true });

const posts = await connection
    .createQueryBuilder()
    .select("user.name", "name")
    .from("(" + userQb.getQuery() + ")", "user")
    .setParameters(userQb.getParameters())
    .getRawMany();

But it’s disappointing that the “more elegant syntax” suggested by the docs doesn’t work.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found