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 6 years ago
  • Reactions: 9
  • Comments: 15 (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?