Search code examples
mysqltypeormnode.js-typeorm

TypeORM (MySQL) Failing to create ManyToOne / OneToMany Relationships


I am following the documentation from TypeORM directly here.

The Entity & relationship I am trying to create is the following:

ReportsEntity
@Entity({name: "reports"})
export class ReportsEntity {
    @PrimaryGeneratedColumn('uuid')
    uuid: string;

    @PrimaryColumn()
    @ManyToOne(() => ApplicationsEntity, (application) => application.reports)
    application: ApplicationsEntity

    @Column()
    description: string

    @Column({ type: 'timestamp' })
    @CreateDateColumn()
    createdAt: Date;

    @Column({ type: 'timestamp' })
    @UpdateDateColumn()
    updatedAt: Date;
}
ApplicationsEntity
@Entity({name: "applications"})
export class ApplicationsEntity {
    @PrimaryGeneratedColumn('uuid')
    uuid: string;

    @Column()
    name: string;

    @Column()
    description: string;

    @Column({ type: 'timestamp' })
    @CreateDateColumn()
    createdAt: Date;

    @Column({ type: 'timestamp' })
    @UpdateDateColumn()
    updatedAt: Date;

    //Relationships
    @OneToMany(() => ReportsEntity, (report) => report.application)
    reports: ReportsEntity[]
}

When launching however, I get the following error from MySQL:

Data type "Object" in "ReportsEntity.application" is not supported by "mysql" database.

I fail to see what I miss or what I am doing wrong when reviewing & comparing the documentation.


Solution

  • remove @PrimaryColumn() above the @ManyToOne, because it's trying to create column with data type object "ApplicationsEntity", which is not supported by mysql

    ReportsEntity

     @ManyToOne(() => ApplicationsEntity, (application) => application.reports)
        application: ApplicationsEntity
    

    ApplicationsEntity

     @OneToMany(() => ReportsEntity, (report) => report.application)
        reports: ReportsEntity[]