TypeORM: How to store BIGINT correctly

Last updated on April 22, 2022 Napoleon Loading... 2 comments

If you’re using TypeORM to work with PostgreSQL or MySQL and want to save BIGINT (from -2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807)) to your database, you have to define your column as follows:

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

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

    @Column()
    email: string;

    // BigInt column here
    @Column({type: 'bigint'})
    bigThing: string;
}

The important thing here is that bigint values are too large to be represented by the number primitive. In TypeORM, they don’t fit into the regular number type and you should map a bigint property to a string instead. If you set number type, you are likely to run into the error: integer out of range.

Further reading:

You can also check out our Node.js topic page and database topic page for the latest tutorials and examples.

Subscribe
Notify of
guest
2 Comments
Inline Feedbacks
View all comments
Samuel
Samuel
4 months ago

How about mssql? It seems that

// BigInt column here
@Column({type: ‘bigint’})
bigThing: string;

it is not working for mssql

A Goodman
Admin
A Goodman
4 months ago
Reply to  Samuel

I’ll give it a shot

You May Also Like