Saving bigints in typeorm to a SQL Server database - Validation failed for parameter '0'. Value must be between -2147483648 and 2147483647, inclusive

Viewed 1338

It seems like tedious (which is used by the node mssql driver) is incorrectly calling regular size int validation on a column specified to be a bigint. I have checked in the SQL Server database and verified that the created column is a bigint, and any queries I do within azure data studio against this db using large ints (e.g. values larger than 4 billion) succeed.

I have this typeorm entity defined:

export class Employee extends BaseEntity {
  @PrimaryColumn('bigint')
  id: number;
...

And when I try to save that employee I receive this error message:

SELECT "Employee"."id" AS "Employee_id", ... FROM "employee" "Employee" WHERE "Employee"."id" IN (@0) -- PARAMETERS: [4382863930]

Error: RequestError: Validation failed for parameter '0'. Value must be between -2147483648 and 2147483647, inclusive

I'm not sure if I'm just missing something or need to specify more information about the column on the entity or if this is a real issue with typeorm

1 Answers

You can use transform feature provided by typeorm. so in this particular case, you can have your code like this,

https://typeorm.io/#/decorator-reference/column

transformer: ValueTransformer|ValueTransformer[] - Specifies a value transformer (or array of value transformers) that is to be used to (un)marshal this column when reading or writing to the database. In case of an array, the value transformers will be applied in the natural order from entityValue to databaseValue, and in reverse order from databaseValue to entityValue.

import { BaseEntity, PrimaryColumn, ValueTransformer } from "typeorm";
export const bigint: ValueTransformer = {
  to: (entityValue: number) => entityValue.toString(),
  from: (databaseValue: string): number => parseInt(databaseValue, 10)
};
export class Employee extends BaseEntity {
  @PrimaryColumn('bigint', { transformer: [bigint] })
  id: number;
}
Related