Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decimal field is transformed to string using createQueryBuilder #7063

Open
1 of 17 tasks
alanscordoba opened this issue Nov 13, 2020 · 8 comments
Open
1 of 17 tasks

Decimal field is transformed to string using createQueryBuilder #7063

alanscordoba opened this issue Nov 13, 2020 · 8 comments

Comments

@alanscordoba
Copy link

alanscordoba commented Nov 13, 2020

Issue type:

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

Database system/driver:

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

When using createQueryBuilder, the decimals are returned as strings.

 return await this.repository.createQueryBuilder("products")
    .select([
      'products.id AS id',
      'products.price AS price'])  
    .getRawMany();

This is my table:

@Entity('products')
export class Products {

    @PrimaryGeneratedColumn()
    @IsNumber()
    public id: number;

    @Column('decimal', { 
      precision: 20,
      scale: 2,
      transformer : { 
        to (value)  {
          return value ; 
        }, 
        from (value)  { 
          return parseFloat (value) ; 
        }, 
      }, 
    })
    @IsNotEmpty()
    price: number;
}

I have already tried with this configuration:

bigNumberStrings: false,
supportBigNumbers: true,

Expected Behavior

I need the decimals to be returned as decimals.

Actual Behavior

Decimals are returned as strings.

@jeiea
Copy link
Contributor

jeiea commented Nov 15, 2020

  • Was this issue not occured in the previous version of typeorm?
    (that is, is it a regression? I modified recently decimal type handling.)
  • You are using transformer, I don't know that feature well.
    Is transformer working with the query builder and other column types?

@alanscordoba
Copy link
Author

alanscordoba commented Nov 16, 2020

  • This problem is happening to me in version 0.2.29. I also clarify that I am using mysql2 version 2.2.5.
  • If I make the query through: return await this.repository.find();
    It works correctly, that is, the data is displayed in decimal format and not in strings.
    Apparently it's with createQueryBuilder where it doesn't work.

@jeiea
Copy link
Contributor

jeiea commented Nov 16, 2020

  • I can't know whether it is a regression from your answer.
  • I used @AfterLoad for the work of that kind. I don't know whether transformer should be called when using query builder. Is it documented?

@jeiea
Copy link
Contributor

jeiea commented Nov 16, 2020

According to #1990, transformer is not applied to query builder at least 2 years ago.
It seems not applied transformer problem is not limited to decimal type field.

@alanscordoba
Copy link
Author

Ok thanks for your answers, but then you mean you can't get decimals using createQueryBuilder, that's really frustrating for me.

@alanscordoba
Copy link
Author

Any news regarding this topic?

@alanscordoba
Copy link
Author

Something new?

@cuongndc
Copy link

any updates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants