【问题标题】:Typeorm Entity Insert Without SynchronizationTypeorm 实体插入不同步
【发布时间】:2020-05-06 16:01:03
【问题描述】:

我正在使用 TypeORM 和 NestJs 来处理现有的 MySql 数据库。我必须将记录插入到一​​个共享表中,该表具有自动递增的主键。由于表是共享的,我已经关闭了同步。

当我使用 @PrimaryGeneratedColumn 装饰器设置我的 TypeORM 实体时,我收到一条错误消息,指出该列“没有默认值”。如果我将装饰器更改为@Column,则会收到关于没有主键的错误。

谁能告诉我应该如何设置我的 TypeORM 实体,以便我可以将一条记录插入到具有自动递增列的表中,其中同步已关闭?

更新

这里是 MySql 表 DDL 的摘录:

CREATE TABLE IF NOT EXISTS tIntegrationItem (
    IntegrationItemID       mediumint AUTO_INCREMENT    NOT NULL,
    Description             varchar(200)                NOT NULL,
    CreatedDateTime         dateTime                    NOT NULL,
    PRIMARY KEY (IntegrationItemID)
) ENGINE=InnoDB;

这是实体的摘录:

@Entity({ name: "tIntegrationItem" })
export class IntegrationItem {
    @PrimaryGeneratedColumn()
    integrationItemId : number;

    @Column()
    description : string;

    @Column()
    createdDateTime  : Date = new Date();
}

这是我收到的错误:

[Nest] 26248 - 2020 年 1 月 22 日下午 2:18:43 [ExceptionsHandler] ER_NO_DEFAULT_FOR_FIELD:字段“IntegrationItemID”没有默认值 +18075ms QueryFailedError: ER_NO_DEFAULT_FOR_FIELD: 字段 'IntegrationItemID' 没有默认值

【问题讨论】:

  • 如需更详细的检查,请添加您现有表的架构描述、实体类和错误日志。

标签: mysql nestjs typeorm


【解决方案1】:

问题可能与列名有关。

integrationItemId : number;

IntegrationItemID

【讨论】:

    【解决方案2】:

    我将数据库配置切换到不同的数据库,并且应用程序运行良好。我将应用程序配置为使用未正确设置的 MySql 本地实例。不过,我不知道确切的原因。

    更新

    我后来得知我的本地数据库与服务器实例不匹配。设置时,主键列未设置为自动递增。 *嘘*

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-15
      • 1970-01-01
      • 2022-07-23
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-10
      相关资源
      最近更新 更多