我一直在遵循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。提前感谢。
5条答案
按热度按时间lbsnaicq1#
这对我很有效--将type属性放到每一列。
7bsow1i62#
对我来说,ORM的配置文件中缺少
metadataProvider
:gmxoilav3#
你需要将这一行添加到你的索引脚本中(到顶部):
(and添加到你的依赖项,如果你还没有它)
bz4sfanl4#
实际上我已经修复了它。看起来像是 typescript 没有正确编译,所以我从src文件夹中删除了Post.js文件,当我再次编译它时,它似乎可以按预期工作。
wydwbb8l5#
我在开发中使用
tsx
运行我的项目时遇到了这个错误。我切换回ts-node-dev
,错误消失了。用tsc
将其编译为JavaScript也可以正常工作。