Search code examples
mysqltypescripttypeorm

"Unknown column 'Test.computerIDComputerID' in 'field list'"


I'm new to typeORM and I have been dealing with this error for a long time. It seems that when I use the function find() typeORM creates a query where a column of a relation is duplicated. For example I have this piece of code:

if(!typeORM.isInitialized)
    await typeORM.initialize()

const test2 = await typeORM.getRepository(Test)
const result  = await test2.find({where: {testID: 98}, relations: {computerID:true}})
console.log(result)

And appears this error:

  code: 'ER_BAD_FIELD_ERROR',
  errno: 1054,
  sqlMessage: "Unknown column 'Test.computerIDComputerID' in 'field list'",
  sqlState: '42S22',
  index: 0,
  sql: 'SELECT `Test`.`testID` AS `Test_testID`, `Test`.`name` AS `Test_name`, `Test`.`testParameters` AS `Test_testParameters`, `Test`.`saveOutput` AS `Test_saveOutput`, `Test`.`deploy` AS `Test_deploy`, `Test`.`computerIDComputerID` AS `Test_computerIDComputerID`, `Test`.`suiteIDSuiteID` AS `Test_suiteIDSuiteID`, `Test__Test_computerID`.`computerID` AS `Test__Test_computerID_computerID`, `Test__Test_computerID`.`ip` AS `Test__Test_computerID_ip`, `Test__Test_computerID`.`name` AS `Test__Test_computerID_name`, `Test__Test_computerID`.`deployPath` AS `Test__Test_computerID_deployPath`, `Test__Test_computerID`.`cpu` AS `Test__Test_computerID_cpu`, `Test__Test_computerID`.`cores` AS `Test__Test_computerID_cores`, `Test__Test_computerID`.`architecture` AS `Test__Test_computerID_architecture`, `Test__Test_computerID`.`ram` AS `Test__Test_computerID_ram`, `Test__Test_computerID`.`hdd` AS `Test__Test_computerID_hdd`, `Test__Test_computerID`.`speed` AS `Test__Test_computerID_speed`, `Test__Test_computerID`.`os` AS `Test__Test_computerID_os`, `Test__Test_computerID`.`user` AS `Test__Test_computerID_user`, `Test__Test_computerID`.`password` AS `Test__Test_computerID_password` FROM `Test` `Test` LEFT JOIN `Computer` `Test__Test_computerID` ON `Test__Test_computerID`.`computerID`=`Test`.`computerIDComputerID` WHERE (`Test`.`testID` = 98)'

And I have these two tables:

@Entity({name:"Test",synchronize: false})
export class Test{
    @PrimaryGeneratedColumn({ type: "int" })
    testID!: number;         
    @Column({ type: "varchar", length: 50, unique: true })
    name!: string;    

    @ManyToOne(() => Computer, computer => computer.tests)
    @JoinTable({ name: 'computerID'})
    computerID!: Computer;

    @ManyToOne(() => Suite, suite => suite.Test)
    @JoinTable({name: "suiteID"})
    suiteID!: Suite;

    @Column({ type: "varchar", length: 300 })
    testParameters!: string;
    @Column({ type: "tinyint" })
    saveOutput!: number;        
    @Column({ type: "varchar", length: 45 })
    deploy!: string;

    @OneToMany(() => Execution, execution => execution.testID,{cascade: ['insert','update']})
    Execution!: Execution[]

    @OneToMany(() => Scheduler, scheduler => scheduler.testID,{cascade: ['insert','update']})
    Scheduler!: Scheduler[]

}

@Entity({name:"Computer",synchronize: false})
export class Computer {
    @PrimaryGeneratedColumn({ type: "int" })
    computerID!: number;
    @Column({ type: "varchar" })
    ip!: string;
    @Column({ type: "varchar",unique: true })
    name!: string;     
    @Column({ type: "varchar"})
    deployPath!: string;     
    @Column({ type: "varchar"})
    cpu!: string;
    @Column({ type: "varchar"})
    cores!: string;
    @Column({ type: "varchar"})
    architecture!: string;
    @Column({ type: "varchar",unique: true })
    ram!: string;     
    @Column({ type: "varchar"})
    hdd!: string;
    @Column({ type: "varchar"})
    speed!: string;
    @Column({ type: "varchar"})
    os!: string;
    @Column({ type: "varchar"})
    user!: string;     
    @Column({ type: "varchar"})
    password!: string;

    @OneToMany(() => Execution, execution => execution.computerID)
    Execution!: Execution[]

    @OneToMany(() => Test, test => test.testID,{cascade: ['insert','update']})
    tests!: Test[]

}

I suppose that the error is in the relations, but I have been doing this kind of relations before and I have never found this error.


Solution

  • I finally found the silly mistake I did. Instead of using JoinTable for the relations of type ManyToOne/OneToMany, I should have used JoinColumn.