question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

docs do not describe how bigint type is treated as string to avoid rounding issues

See original GitHub issue

Issue type:

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

Database system/driver:

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

TypeORM version:

[ ] latest [ ] @next [x] 0.2.7 (or put your version here)

Steps to reproduce or a small repository showing the problem:

I ran console.log(entity, typeof entity.myBigIntColumn) and noticed that my column was a string instead of Number. This makes sense because the maximum postgres bigint value is 9223372036854775807 and yet in JavaScript Number.MAX_SAFE_INTEGER is 9007199254740991. However, the documentation at http://typeorm.io/#/entities/column-types-for-postgres doesn’t mention that at all.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:33
  • Comments:27 (5 by maintainers)

github_iconTop GitHub Comments

34reactions
albrowcommented, Nov 28, 2018

I just wanted to point out that this issue means that the type definitions for methods like find and findOne are incorrect. If you have an entity that looks like this:

@Entity()
export class Person {
    @PrimaryColumn() public name!: string;

    @Column({ type: 'bigint' })
    public age!: number;
}

And you call findOne like this:

const person = await personRepository.findOne({where: {name: 'Alice'}});

The TypeScript compiler thinks that person has type Person because that is what the type definition for findOne specifies. But in reality the type of the person.age will be string instead of number. This can lead to some surprising and confusing errors.

31reactions
DanielLoyAugmedixcommented, Feb 5, 2020

I’ll leave some bread crumbs because I got this to work for my application.

In creating the the TypeOrm connection, I’m using Nest but that doesn’t matter, you can add the option

      bigNumberStrings: false,

And typescript 3.2 supports bigint types.

We can therefor declare:

  @PrimaryGeneratedColumn({type: 'bigint'})
  public id: bigint;

This works for me and presents no risk of overflow. I hope this helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: 8.1. Numeric Types - PostgreSQL
Rounding might take place if the precision of an input number is too high. Numbers too close to zero that are not representable...
Read more >
Decimal rounding issue in Informatica
There's no way (except with String datatypes) to cater for numbers with a higher number of digits. Everything above 28 digits will automatically...
Read more >
Let's talk about big numbers in JavaScript - ITNEXT
Decimals with arbitrary precision can't be represented using the Number data type, as they will just get rounded to a smaller format.
Read more >
math/big - Go Packages
Rounding is performed according to z's precision and rounding mode; and z's accuracy reports the result error relative to the exact (not rounded)...
Read more >
BigDecimal (Java Platform SE 8 ) - Oracle Help Center
If no rounding mode is specified and the exact result cannot be ... setting is not 0, the rules of BigDecimal arithmetic are...
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