Reputation: 107
I tried to mikroORM create migrations but it seems that I cannot create the table itself. I don't know what I missed and the error says database "crm" does not exist.
Please see code below:
mikro-config.ts
import { MikroORM } from "@mikro-orm/core";
import { Post } from "./entities/Post";
import { __prod__ } from "./constants";
import path from "path";
export default {
migrations: {
path: path.join(__dirname, "./migrations"), // path to the folder with migrations
pattern: /^[\w-]+\d+\.[tj]s$/, // regex pattern for the migration files
},
entities: [Post],
dbName: "crm",
type: "postgresql",
user: "postgres",
debug: !__prod__,
} as Parameters<typeof MikroORM.init>[0];
index.ts
import { MikroORM as MK } from "@mikro-orm/core";
import { __prod__ } from "./constants";
import { Post } from "./entities/Post";
import mikroOrmConfig from "./mikro-orm.config";
const main = async () => {
const orm = await MK.init(mikroOrmConfig);
const post = orm.em.create(Post, {
firstName: "Test",
lastName: "Test2",
dateOfBirth: "2012-01-01",
email: "[email protected]",
phone: "192803994",
address: "34th street local avenue, trade city",
});
await orm.em.persistAndFlush(post);
};
main().catch((err) => console.log(err));
console.log("test");
Post.ts
import { Entity, PrimaryKey, Property } from "@mikro-orm/core";
@Entity()
export class Post {
@PrimaryKey()
_id!: number;
@Property()
createdAt: Date = new Date();
@Property({ onUpdate: () => new Date() })
updatedAt: Date = new Date();
@Property()
firstName!: string;
@Property()
lastName!: string;
@Property()
dateOfBirth!: Date;
@Property()
email!: string;
@Property()
phone!: string;
@Property()
Address!: string;
}
npx mikro-orm migration:create --initial.
I've already delete the folders and tried again nothing happen.
Upvotes: 1
Views: 1040
Reputation: 18389
You just created the migration, now you need to execute it via npx 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-via-cli
Alternatively you can use SchemaGenerator
directly instead of migrations:
npx mikro-orm schema:create --run # creates the schema, including the database if not exists
npx mikro-orm schema:update --run # updates the schema
npx mikro-orm schema:drop --run # drops the schema
https://mikro-orm.io/docs/schema-generator/
Upvotes: 1
Reputation: 531
You need to create a postgres database separately. Mikro-orm will not create a database for you.
Create a postgres database with name "crm" and then run and apply migrations your tables will be created.
Upvotes: 0