'mikro-orm migration is not reflected in postgresql
this is User entity:
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;
}
this is the migration script:
"create:migration": "mikro-orm migration:create"
I already had username and password column but when I tried to add "email" column, migration runs successfully but it does not reflected in database. This is the migration result:
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");');
}
}
I refresh table but do not see email column. When I send client request I get this error:
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
When I run npx mikro-orm migration:up this time I get this message:
"DriverException: alter table "user" add column "email" text not null; - column "email" of relation "user" already exists"
somehow migration is not modifying the database
Solution 1:[1]
i found a way around it. we are probably following the same tutorial by ben awad.what i did was erase the whole User table from the postgres database. then i commented out the following command from index.ts.
//await orm.getMigrator().up()
later on i deleted all the previous migrations.
then i typed in terminal :
npx mikro-orm migration:create
then:
npx mikro-orm migration:validate
in order to make sure that the correct migration is about to be implemented
and then:
npx mikro-orm migration:up
In my opinion it seems like there is a problem with the migrator and the migration issues. i still haven't used the automatic migrator tool orm.getMigrator().up() but i may try it again in the future
Solution 2:[2]
You just created the migration, now you need to run it via 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
Solution 3:[3]
Make sure that the User entity is listed in options.entities inside mikro-orm.config.js file.
Solution 4:[4]
If you are like me watching Ben Awad's youtube video. I encounter this problem too. What I do was first searching in the discord form, and found that that the quickest way to resolve this is to drop/delete all tables in the database as well as migration files (both ts and .js file in dist folder as mentioned here ).
Then run npx mikro-orm migration:create, yarn dev then you are good to go.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | shauna vayne |
| Solution 2 | Martin Adámek |
| Solution 3 | 4b0 |
| Solution 4 | daolanfler |
