NodeJS 如何解决在MikroORM中创建迁移时“请在www.example.com中提供'type'或'entity'属性EntityName.id”错误?

cyej8jka  于 2023-04-11  发布在  Node.js
关注(0)|答案(5)|浏览(101)

我一直在遵循Ben Awad Fullstack教程,遇到了一个问题。所以,我已经安装了MikroORM并使用PostgreSQL数据库。问题是npx mikro-orm migration:create命令返回以下错误:

Error: Please provide either 'type' or 'entity' attribute in Post.id
    at ReflectMetadataProvider.initPropertyType (F:\Web Development\RedditClone\node_modules\@mikro-orm\core\metadata\ReflectMetadataProvider.js:14:19)
    at F:\Web Development\RedditClone\node_modules\@mikro-orm\core\metadata\ReflectMetadataProvider.js:9:54
    at ReflectMetadataProvider.initProperties (F:\Web Development\RedditClone\node_modules\@mikro-orm\core\metadata\MetadataProvider.js:26:23)
    at ReflectMetadataProvider.loadEntityMetadata (F:\Web Development\RedditClone\node_modules\@mikro-orm\core\metadata\ReflectMetadataProvider.js:9:20)
    at MetadataDiscovery.discoverEntity (F:\Web Development\RedditClone\node_modules\@mikro-orm\core\metadata\MetadataDiscovery.js:182:41)
    at MetadataDiscovery.discoverReferences (F:\Web Development\RedditClone\node_modules\@mikro-orm\core\metadata\MetadataDiscovery.js:131:24)
    at MetadataDiscovery.findEntities (F:\Web Development\RedditClone\node_modules\@mikro-orm\core\metadata\MetadataDiscovery.js:71:20)
    at MetadataDiscovery.discover (F:\Web Development\RedditClone\node_modules\@mikro-orm\core\metadata\MetadataDiscovery.js:34:9)
    at Function.init (F:\Web Development\RedditClone\node_modules\@mikro-orm\core\MikroORM.js:40:24)

我创建了一个Post实体,看起来像这样:

import { Entity, PrimaryKey, Property } from "@mikro-orm/core";

@Entity()
export class Post {
    @PrimaryKey({ type: "number" })
    id!: number;

    @Property()
    createdAt = new Date();

    @Property({ onUpdate: () => new Date() })
    updatedAt = new Date();

    @Property()
    title!: string;
}

我还为MikroORM创建了如下配置文件:

import { __prod__ } from "./constants";
import { Post } from "./entities/Post";
import { MikroORM } from "@mikro-orm/core";
import path from "path";

export default {
    migrations: {
        path: path.join(__dirname, "./migrations"),
        pattern: /^[\w-]+\d+\.[tj]s$/,
    },
    entities: [Post],
    dbName: "redditclone",
    type: "postgresql",
    debug: !__prod__,
    user: "postgres",
    password: "postgres",
} as Parameters<typeof MikroORM.init>[0];

所有这些都被放在index.ts脚本中:

import { MikroORM } from "@mikro-orm/core";
import { __prod__ } from "./constants";
import { Post } from "./entities/Post";
import microConfig from "./mikro-orm.config";

const main = async () => {
    const orm = await MikroORM.init(microConfig);

    const post = orm.em.create(Post, { title: "my first post" });
    await orm.em.persistAndFlush(post);
};

main();

我已经尝试将Post实体id更改为uuid,并将相应的object参数添加到@PrimaryKey装饰器中,但错误保持不变。目前我只使用MikroORM和ts-node dependecies。提前感谢。

lbsnaicq

lbsnaicq1#

这对我很有效--将type属性放到每一列。

import { Entity, PrimaryKey, Property } from '@mikro-orm/core'

@Entity()
export class Post {
  @PrimaryKey({ type: 'number' })
  id!: number

  @Property({ type: 'date' })
  createdAt: Date = new Date()

  @Property({ type: 'date', onUpdate: () => new Date() })
  updatedAt: Date = new Date()

  @Property({ type: 'text' })
  title!: string
}
7bsow1i6

7bsow1i62#

对我来说,ORM的配置文件中缺少metadataProvider

// orm.config.ts

import { MikroORM } from '@mikro-orm/core';
import { TsMorphMetadataProvider } from '@mikro-orm/reflection';

export default {
    // ...
    metadataProvider: TsMorphMetadataProvider
    // ...
} as Parameters <typeof MikroORM.init> [0];
gmxoilav

gmxoilav3#

你需要将这一行添加到你的索引脚本中(到顶部):

import 'reflect-metadata';

(and添加到你的依赖项,如果你还没有它)

bz4sfanl

bz4sfanl4#

实际上我已经修复了它。看起来像是 typescript 没有正确编译,所以我从src文件夹中删除了Post.js文件,当我再次编译它时,它似乎可以按预期工作。

wydwbb8l

wydwbb8l5#

我在开发中使用tsx运行我的项目时遇到了这个错误。我切换回ts-node-dev,错误消失了。用tsc将其编译为JavaScript也可以正常工作。

相关问题