Search code examples
postgresqlnestjstypeorm

"QueryFailedError: invalid input syntax for integer:" when querying on type float


When querying the database using the TypeORM QueryBuilder() I get:

QueryFailedError: invalid input syntax for integer: "X"

With X being the value stored in the DB.

Originally my entity was of type;

{type: 'decimal', precision: 5, scale: 2 }
value: number

Since I have changed it to:

{type: 'real'}
value: string

and tried:

'float'
value: string

All three types throw the same error. However, if the value in the DB doesn't have any decimal places - it works fine.

I am running Postgres v11.4, TypeORM v0.2.18 and Nest.js v6.5.3

The entity definition:

export class Entity extends BaseEntity {

    @Column('float')
    value: string;
}

The query:

const current = await this.entityRepo
            .createQueryBuilder('uL')
            .leftJoin('uL.user', 'user')
            .leftJoinAndSelect('uL.currentLevel', 'cL')
            .where('user.id = :id', { id: userId })
            .getOne();

I am expecting the entity to be returned with the value being of correct decimal spacing.


Solution

  • I faced the same error, after updating to TypeORM v0.2.18: PostgreSQL doesn't actually know "number"; more on that in the typeorm repository on Microsoft GitHub

    Changed this:

    @Column()
    value: number;
    

    ... to that:

    @Column({type: 'real'})
    value: string;
    

    I forgot to migrate; did you?

    Important Run a migration, to actually apply these changes:

    npm run typeorm:migrate ChangedNumbersTypeToReal
    npm run build; npm run typeorm:run
    

    Everything should be well now.

    You can read more about migrations with TypeORM on their GitHub