简体   繁体   English

TypeORM 中的 ColumnTypeUndefinedError

[英]ColumnTypeUndefinedError in TypeORM

Can someone tell me why, on launch my project i get this error:有人能告诉我为什么,在启动我的项目时,我得到这个错误:

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.

here is my column like above,这是我上面的专栏,

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

thanks for any help!谢谢你的帮助!

It is because the database does not know about User type.这是因为数据库不知道User类型。

Change User to string and the error will go away, because the database knows about string :User更改为string ,错误将 go 消失,因为数据库知道string

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

If you want User instead of string , use @ManyToOne instead of @Column.如果您想要User而不是string ,请使用 @ManyToOne 而不是 @Column。 This sets up a foreign key relationship to the User table, and if you add eager: true the User entity will be loaded automatically with the base entity when you use any of the TypeOrm find* methods:这将设置与 User 表的外键关系,如果您添加eager: true ,则当您使用任何 TypeOrm find*方法时, User实体将与基本实体一起自动加载:

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

you are getting this error because your tsconfig.json file is not properly configured.您收到此错误是因为您的tsconfig.json文件配置不正确。 add the following to your file.将以下内容添加到您的文件中。

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

then you have to run那么你必须运行

npm run tsc npm运行tsc

after you've editted your tsconfig file, for recompilation.编辑完 tsconfig 文件后,进行重新编译。

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

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