I am currently navigating through the syntax of typeORM and have been stuck troubleshooting an issue for quite some time. It appears that whenever I utilize the find() function in typeORM, a query is generated with a duplicated column from a relation. Here is a snippet of the code I've been working on:
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)
When running this code, I encounter the following 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)'
This leads me to my two table entities:
@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[]
}
My suspicion lies within the relational structure, as I have successfully implemented similar relationships in the past without encountering such errors.