【问题标题】:Nestjs problem creating mysql table from entityNestjs问题从实体创建mysql表
【发布时间】:2020-03-18 20:16:20
【问题描述】:

我在更改实体文件并且 Nest/TypeOrm 重新生成 mysql 表时遇到问题。它不反映我在实体文件中的内容

这是我的 user.entity.ts

import { BaseEntity, Entity, Column, Unique, PrimaryGeneratedColumn } from "typeorm";

@Entity()
@Unique(['id'])
export class User extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  uid: string;

  @Column()
  name: string;

  @Column()
  surname: string;

  @Column()
  email: string;

  @Column()
  skype: string;

  @Column()
  levelClassDone: boolean;

  @Column({default: 1, nullable: true})
  userType: number;
}

这是mysql中创建表的描述:

+----------------+--------------+------+-----+---------+-------+
| Field          | Type         | Null | Key | Default | Extra |
+----------------+--------------+------+-----+---------+-------+
| name           | varchar(255) | NO   |     | NULL    |       |
| surname        | varchar(255) | NO   |     | NULL    |       |
| email          | varchar(255) | NO   | UNI | NULL    |       |
| uid            | varchar(255) | NO   |     | NULL    |       |
| skype          | varchar(255) | NO   |     | NULL    |       |
| levelClassDone | tinyint      | NO   |     | NULL    |       |
| userType       | int          | YES  |     | 1       |       |
| id             | varchar(255) | NO   | PRI | NULL    |       |
+----------------+--------------+------+-----+---------+-------+

id 不是 numeric 也不是 auto_increment

似乎它正在缓存我有不同字段规范的旧 user.entity.ts 版本。我试图删除表并删除整个数据库。

【问题讨论】:

  • 请检查您的 ORM 配置:您可能正在使用一些“旧编译”文件。 entities 属性非常重要,它必须匹配您的目标实体文件,并且这些文件(不是副本,或旧/编译的 .js 文件)。您还可以尝试同时启用“dropSchema”和“synchronize”,以确保每次尝试都使用新的架构。

标签: nest nestjs typeorm


【解决方案1】:

在将 TypeORM 导入到您的模块时,尝试提供 autoLoadEntitiessynchronize 选项:

    TypeOrmModule.forRoot({
      autoLoadEntities: true,
      synchronize: true,
      ...
    }

【讨论】:

  • 我设置了 synchronize: trueautoLoadEntities 未被识别为选项。我尝试了不同的选项,例如 cache: false
猜你喜欢
  • 2019-11-13
  • 2020-08-27
  • 2020-03-05
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
  • 2014-04-26
  • 2012-02-25
  • 2015-02-12
相关资源
最近更新 更多