【问题标题】:Cannot insert bigint in SQL Server using typeorm (NestJS)无法使用 typeorm (NestJS) 在 SQL Server 中插入 bigint
【发布时间】:2022-01-02 05:59:41
【问题描述】:

我想使用 typeorm 在 SQL Server 中插入记录,其中列的类型为 bigint。我有 id 类型的“猫”实体:bigint。

import { Column, Entity } from 'typeorm';

@Entity('Cats')
export class CatsEntity {
  @Column({ type: 'bigint', name: 'CatID' })
  public id: string;

  @Column('int', { primary: true, name: 'CatDB' })
  public db: number;

  @Column('varchar', { name: 'Name' })
  public name: string;

  @Column('datetime', { name: 'DDB_LAST_MOD' })
  public ddbLastMod: Date;
}

还有 dto,我进入控制器:

export class InsertCatsDto {
  public id: string;

  public db: number;

  public name: string;
}

在控制器中执行保存:

@Post('/cats')
  public async insertEobResponse(@Body() insertCatsDto: InsertCatsDto): Promise<any> {
    const cats = new CatsEntity();

    cats.id = insertCatsDto.id;
    cats.db = insertCatsDto.db;
    cats.name = insertCatsDto.name;
    cats.ddbLastMod = new Date();

    return this.catsRepository.insert(cats);
  }

但是当我通过 Postman 发送带有 id 作为字符串的请求时,我收到以下错误:

"error": "Error: Validation failed for parameter '0'. Value must be between -9007199254740991 and 9007199254740991, inclusive. For smaller or bigger numbers, use VarChar type."

我不确定我是否只是缺少某些东西或需要对值进行一些转换,或者这是否是 typeorm 的一个真正问题。

【问题讨论】:

标签: sql-server nestjs typeorm


【解决方案1】:

要使您的代码在 typeorm 中使用 bigInt,您只需将实体中的类型从“bigint”更改为“varchar”:

import { Column, Entity } from 'typeorm';

@Entity('Cats')
export class CatsEntity {
@Column({ type: 'varchar', name: 'CatID' })
public id: string;

@Column('int', { primary: true, name: 'CatDB' })
public db: number;

@Column('varchar', { name: 'Name' })
public name: string;

@Column('datetime', { name: 'DDB_LAST_MOD' })
public ddbLastMod: Date;
}

【讨论】:

    猜你喜欢
    • 2021-11-06
    • 2014-08-17
    • 2020-06-19
    • 2020-09-01
    • 2019-03-07
    • 2020-04-13
    • 2014-03-22
    • 1970-01-01
    相关资源
    最近更新 更多