TypeORM group by using QueryBuilder not working

Viewed 878

I want to create a resulset using TypeORM's QueryBuilder but I am running into all kinds of errors trying to do so.

This is the entity my question is about

@Entity()
export class Balance {
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToOne(() => Address, address => address.balance, {onDelete: "CASCADE"})
  address: Address;

  @ManyToOne(() => Token, token => token.symbol, {
    eager: true,
    onDelete: "CASCADE",
  })
  @JoinColumn()
  token: Token;

  @Column("numeric") //, { precision: 78, scale: 0, default: 0.00 })
  amount: number;

  @CreateDateColumn()
  created_at: Date;

  @UpdateDateColumn()
  updated_at: Date;
}

I want to sum the "amount" of each balance grouped by the the tokenId so the desired output would looks like this

[{amount:323423424242, token: tokenId/or token.symbol }]

This is my repository class

import { createQueryBuilder, EntityRepository, Repository } from "typeorm";
import { Address } from "../../../entity";
import { Balance } from "../../../entity/Balance";

@EntityRepository(Balance)
export class BalanceRepository extends Repository<Balance> {
  async findByAddress(address: Address): Promise<any> {
    return this.createQueryBuilder("balance").leftJoinAndSelect("balance.token", "token").addSelect("SUM(balance.amount", "amount").groupBy("balance.tokenId").getRawMany();

  }
}

@Entity() 
export class Token { 
   
   @PrimaryGeneratedColumn() 
   id: number; 
   
   @Column() 
   symbol: string; 
   
   @Column() 
   name: string; 

   @Column() 
   decimals: number; 

   @Column({unique:true, nullable:false}) 
   address: string; 

   @Column({nullable:true}) 
   logoURI: string; 

   @CreateDateColumn()
   created_at: Date;

   @UpdateDateColumn()
   updated_at: Date;
   
}

Later on, I wanted to build up on it to also just excecute this for the given address using a where clause.

The error I am getting right now is this

"query": "SELECT \"balance\".\"id\" AS \"balance_id\", \"balance\".\"amount\" AS \"balance_amount\", \"balance\".\"created_at\" AS \"balance_created_at\", \"balance\".\"updated_at\" AS \"balance_updated_at\", \"balance\".\"addressId\" AS \"balance_addressId\", \"balance\".\"tokenId\" AS \"balance_tokenId\", \"token\".\"id\" AS \"token_id\", \"token\".\"symbol\" AS \"token_symbol\", \"token\".\"name\" AS \"token_name\", \"token\".\"decimals\" AS \"token_decimals\", \"token\".\"address\" AS \"token_address\", \"token\".\"logoURI\" AS \"token_logoURI\", \"token\".\"created_at\" AS \"token_created_at\", \"token\".\"updated_at\" AS \"token_updated_at\", SUM(\"balance\".\"amount\" AS \"amount\" FROM \"balance\" \"balance\" LEFT JOIN \"token\" \"token\" ON \"token\".\"id\"=\"balance\".\"tokenId\" GROUP BY \"balance\".\"tokenId\"",
    "parameters": [],
    "driverError": {
        "length": 92,
        "name": "error",
        "severity": "ERROR",
        "code": "42601",
        "position": "587",
        "file": "scan.l",
        "line": "1145",
        "routine": "scanner_yyerror"
    },
    "length": 92,
    "severity": "ERROR",
    "code": "42601",
    "position": "587",
    "file": "scan.l",
    "line": "1145",
    "routine": "scanner_yyerror"
}

0 Answers
Related