ColumnTypeUndefinedError in TypeORM

Viewed 3512

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!

2 Answers

you are getting this error because your tsconfig.json file is not properly configured. add the following to your file.

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

then you have to run

npm run tsc

after you've editted your tsconfig file, for recompilation.

It is because the database does not know about User type.

Change User to string and the error will go away, because the database knows about string:

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

If you want User instead of string, use @ManyToOne instead of @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:

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