不生成 TypeOrm 迁移

TypeOrm migrations not generating

我对 TypeOrm 还很陌生,我正在尝试将它用于一个项目,但我在生成迁移时遇到了问题。我在本地端口 5432 上有一个 Postgres 应用 运行ning。

我的实体看起来像这样:

@Entity()
export class User {

  @PrimaryGeneratedColumn()
  id!: number;

  @Index({ unique: true })
  @Column('text', {nullable:true})
  username!: string;

  @Index({ unique: true })
  @Column('text', {nullable:true})
  @IsEmail()
  email!: string;

  @Index()
  @Column('bool',{nullable:true, default: false})
  emailVerified!: boolean;


  @UpdateDateColumn()
  @IsDate()
  updateDate!: Timestamp;

  @Column('text',)
  about!: string;
};    

我的 OrmConfig 看起来像这样

{
  "entities": ["src/entity/*.js"],
  "migrations": ["src/migration/*.js"],
  "subscribers": ["src/subscriber/**/*.js"],
  "cli": {
    "migrationsDir": "src/migration",
    "entitiesDir": "src/entities",
    "subscribersDir": "src/subscriber"
  },  
  "url": "postgres://@0.0.0.0:5432/nftme_dev",
  "type": "postgres",
  "synchronize": true
}

当我运行:

yarn run typeorm migration:generate -n initial

它抛出一个错误说:

$ node_modules/.bin/typeorm migration:generate -n initial
No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migration:create" command```

TypeORM 提供了如此多的可能性,很容易迷失在其中。 ;-)

您可以修改本地数据库并生成迁移文件:

typeorm migration:generate -n UserMigration

您可以修改实体并同步更改:(您需要这个)

typeorm schema:sync

调试了一天终于搞定了。

所以当我使用 synchronize: true 选项时,模式已经为我在数据库中的实体创建了,当我试图生成它时失败了,因为没有任何改变。

我创建了一个新的数据库并尝试生成迁移并且成功了。