【问题标题】:Database does not exist when migration create in mikroORM在 mikroORM 中创建迁移时数据库不存在
【发布时间】:2021-11-19 03:04:29
【问题描述】:

我尝试使用 mikroORM 创建迁移,但似乎我无法创建表本身。我不知道我错过了什么,错误说数据库“crm”不存在。

请看下面的代码:

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: "xxy@gmail.com",
    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 迁移:创建 --initial.

我已经删除了文件夹并再次尝试没有任何反应。

【问题讨论】:

    标签: typescript configuration mikro-orm


    【解决方案1】:

    您需要单独创建一个 postgres 数据库。 Mikro-orm 不会为您创建数据库。

    创建一个名为“crm”的 postgres 数据库,然后运行并应用迁移,您的表将被创建。

    【讨论】:

      【解决方案2】:

      您刚刚创建了迁移,现在您需要通过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

      您也可以直接使用SchemaGenerator 而不是迁移:

      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/

      【讨论】:

        猜你喜欢
        • 2018-09-16
        • 1970-01-01
        • 2021-01-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-29
        • 2012-04-10
        • 1970-01-01
        相关资源
        最近更新 更多