【问题标题】:how to proper handling typeorm entity columns in nestjs如何正确处理nestjs中的typeorm实体列
【发布时间】:2021-06-17 19:39:45
【问题描述】:

我在编码中处理实体列时遇到了一些困难,我的意思是从代码中添加或删除/删除它们会导致数据库中出现一些问题,这就是我尝试做的方式

@Entity('user')
export class User {
  ....  other colums
  @Column()
  name: string;
}

以上代码将在数据库中生成user 表,其中包含适当的列,即名称,

现在这是我稍后决定将列 name 更改为 fullname 时的问题

我收到以下错误 QueryFailedError: column "fullname" of relation "user" contains null values

即使我删除user 表并重新运行应用程序,

请帮忙!

【问题讨论】:

    标签: nestjs typeorm pg


    【解决方案1】:

    我想我找到了快速解决方案,我不得不删除 distfolder 并且一切正常

    【讨论】:

      【解决方案2】:

      要使用 TypeORM 处理数据库修改,您可以使用两种解决方案,同步自动数据库更改和迁移手动数据库更改。

      Synchronize
      将 synchronize 参数设置为 true 以自动更新数据库。

      TypeOrmModule.forRoot({
            type: 'mysql',
            host: 'localhost',
            port: 3306,
            username: 'root',
            password: 'root',
            database: 'test',
            entities: [],
            synchronize: true,
          }),
      

      警告
      设置 synchronize: true 不应在生产中使用 - 否则您可能会丢失生产数据。

      Learn more about synchronize

      Migrations
      这个解决方案是最常用的,必须在生产中使用。

      yarn typeorm migration:generate -n migrationName

      这将为您生成一个迁移文件,其中包含基于您的实体的数据库说明。您只需运行迁移以应用数据库更改。

      yarn typeorm migration:up

      Learn more about migrations

      【讨论】:

        猜你喜欢
        • 2018-07-28
        • 2020-03-18
        • 2020-12-22
        • 2020-06-02
        • 1970-01-01
        • 2019-11-13
        • 2022-07-24
        • 1970-01-01
        • 2018-09-11
        相关资源
        最近更新 更多