这是用户实体:
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" 不知何故,迁移并没有修改数据库
发布于 2021-08-09 20:03:30
我找到了绕过它的方法。我们可能正在遵循ben awad.what的相同教程,我所做的就是从postgres数据库中删除整个用户表。然后,我从index.ts中注释掉了以下命令。
//await orm.getMigrator().up()
后来,我删除了所有以前的迁移。
然后我输入了终端:
npx mikro-orm migration:create然后:
npx mikro-orm migration:validate为了确保即将实现正确的迁移
然后:
npx mikro-orm migration:up在我看来,这似乎是迁移程序和迁移问题的问题。我还没有使用自动迁移工具orm.getMigrator().up(),但我以后可能会再试一次
发布于 2021-06-09 03:01:06
您刚刚创建了迁移,现在需要通过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发布于 2021-08-11 04:22:58
确保用户实体在mikro-orm.config.js文件中的options.entities中列出。
https://stackoverflow.com/questions/67892008
复制相似问题