typeorm: Bug in queryBuildier when summing

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [x] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [x] 0.3.0-alpha.7, 0.3.0-alpha.10, 0.2.7

Steps to reproduce or a small repository showing the problem:

import {
  Column,
  ConnectionOptions,
  createConnection,
  Entity,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn
} from "typeorm";

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

const options: ConnectionOptions = {
  type: "mysql",
  host: "localhost",
  port: 6667,
  username: "test",
  password: "test",
  database: "test",

  synchronize: true,
  entities: [User]
};

createConnection(options).then(
  async connection => {
    await connection.synchronize(true);

    const u1 = new User();

    await connection.getRepository(User).save(u1);

    {
      const raw = await connection
        .getRepository(User)
        .createQueryBuilder("user")
        .select("SUM(user.id)", "sum")
        .getRawOne();

      const { sum } = raw;

      console.log("typeof sum", typeof sum); /// "string" ???
      console.log("sum === 1", sum === 1); // false becuase "1" !== 1
      console.log("res", raw);
    }

    {
      const raw = await connection
        .getRepository(User)
        .createQueryBuilder("user")
        .where("user.id = :id", { id: 1 })
        .getRawOne();

      console.log("typeof raw.user_id", typeof raw.user_id); // "number" - ok
      console.log("raw.user_id === 1", raw.user_id === 1); // true - ok
    }
  },
  error => console.log("Cannot connect: ", error)
);

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 9
  • Comments: 16 (5 by maintainers)

Commits related to this issue

Most upvoted comments

What is the state of this bug? Is there anything I could do instead of mapping the result?

In other words, you can sort of work around this by setting bigNumberStrings to false

Do note, however, that disabling bigNumberStrings means that numbers outside the integer threshold for number will become a floating point number! This is very dangerous because it means the number you receive back MAY NOT be exact.

This is not a bug - SUM(user.id) returns a BIGINT and TypeORM enables bigNumberStrings by default.

The standard mysql library does NOT enable bigNumberStrings by default.