繁体   English   中英

TypeORM 中的 ColumnTypeUndefinedError

[英]ColumnTypeUndefinedError in TypeORM

有人能告诉我为什么,在启动我的项目时,我得到这个错误:

ColumnTypeUndefinedError: Column type for Order#author is not defined and cannot be guessed. Make sure you have turned on an "emitDecoratorMetadata": true option in tsconfig.json. Also make sure you have imported "reflect-metadata" on top of the main entry file in your application (before any entity imported).If you are using JavaScript instead of TypeScript you must explicitly provide a column type.

这是我上面的专栏,

    @Column({
        nullable: true
    })
    author: User;

谢谢你的帮助!

这是因为数据库不知道User类型。

User更改为string ,错误将 go 消失,因为数据库知道string

@Column({
        nullable: true
    })
    author: string;

如果您想要User而不是string ,请使用 @ManyToOne 而不是 @Column。 这将设置与 User 表的外键关系,如果您添加eager: true ,则当您使用任何 TypeOrm find*方法时, User实体将与基本实体一起自动加载:

@ManyToOne(() => User, { eager: true })
author: User;

您收到此错误是因为您的tsconfig.json文件配置不正确。 将以下内容添加到您的文件中。

{
    "emitDecoratorMetadata": true,
    "lib": ["es5", "es6", "dom"],  
    "moduleResolution": "node",
    "target": "es5", 
}

那么你必须运行

npm运行tsc

编辑完 tsconfig 文件后,进行重新编译。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM