mikro-orm迁移没有反映在postgresql中



这是用户实体:

import { Entity, PrimaryKey, Property } from "@mikro-orm/core";
import { ObjectType, Field } from "type-graphql";
@ObjectType()
@Entity()
export class User {
@Field()
@PrimaryKey()
id!: number;
@Field(() => String)
@Property({ type: "date" })
createdAt = new Date();
@Field(() => String)
@Property({ type: "date", onUpdate: () => new Date() })
updatedAt = new Date();
@Field()
@Property({ type: "text", unique: true })
username!: string;
@Field()
@Property({ type: "text", unique: true })
email!: string;
@Property({ type: "text" })
password!: string;
}

这是迁移脚本:

"create:migration": "mikro-orm migration:create"

我已经有了用户名和密码栏,但当我尝试添加"时;电子邮件";列,迁移成功运行,但未反映在数据库中。这是迁移结果:

export class Migration20210608151159 extends Migration {
async up(): Promise<void> {
this.addSql('alter table "user" add column "email" text not null;');
this.addSql('alter table "user" add constraint "user_email_unique" unique ("email");');
}
}

我刷新了表格,但没有看到电子邮件列。当我发送客户端请求时,我会收到以下错误:

error: insert into "user" as "e0" ("created_at", "email", "password", "updated_at", "username") values ($1, $2, $3, $4, $5) returning * - column "email" of relation "user" does not exist

当我这次运行npx mikro-orm migration:up时,我会收到以下消息:

"DriverException: alter table "user" add column "email" text not null; - column "email" of relation "user" already exists" 

不知何故,迁移并没有修改数据库

我找到了解决方法。我们可能遵循ben awad的相同教程。我所做的是从postgres数据库中删除整个User表。然后我从index.ts.中注释出以下命令

//await orm.getMigrator().up()

后来我删除了以前的所有迁移。

然后我输入了终端:

npx mikro-orm migration:create

然后:

npx mikro-orm migration:validate

以确保即将实现正确的迁移

然后:

npx mikro-orm migration:up

在我看来,移民和移民问题似乎有问题。我还没有使用自动迁移工具orm.getMigrator((.up((,但我可能会在未来的中再次尝试

您刚刚创建了迁移,现在需要通过mikro-orm migration:up运行它。

npx mikro-orm migration:create   # Create new migration with current schema diff
npx mikro-orm migration:up       # Migrate up to the latest version
npx mikro-orm migration:down     # Migrate one step down
npx mikro-orm migration:list     # List all executed migrations
npx mikro-orm migration:pending  # List all pending migrations

https://mikro-orm.io/docs/migrations/#using-通过cli

确保用户实体列在mikro-orm.config.js文件内的options.entities中。

如果你和我一样在看Ben Awad的youtube视频。我也遇到这个问题。我首先在discord表单中搜索,发现解决这个问题的最快方法是删除数据库中的所有表以及迁移文件(如前所述,dist文件夹中的ts和.js文件(。然后运行npx mikro-orm migration:createyarn dev,就可以开始了。

最新更新