typeorm - .leftJoinAndSelect('itemTrl.itemDowntimes', 'itemDowntimes')
.leftJoinAndSelect('itemDowntimes', 'itemDowntimes.workOrder') i need get workOrder from itemDowntimes (many to many) but i get "syntax error at or near "WHERE""
Assistant
In QueryBuilder the first argument of leftJoinAndSelect is always
“”, while the argument is the alias you want to give to that joined table.
You accidentally swapped them:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
// ❌ wrong order – alias first, path second
.leftJoinAndSelect('itemDowntimes', 'itemDowntimes.workOrder')
Because of the swap TypeORM produced an invalid SQL fragment; Postgres then complains
with syntax error at or near "WHERE" (the malformed JOIN is placed right before the
WHERE clause).
Fix: put the relation path first and use a fresh alias for the new table:
ts
createQueryBuilder('itemTrl')
.leftJoinAndSelect('itemTrl.itemDowntimes', 'itemDowntime') // alias #1
.leftJoinAndSelect('itemDowntime.workOrder', 'workOrder') // alias #2
// .where(...) ...
.getMany();
• itemTrl.itemDowntimes – one-to-many or many-to-many from ItemTrl to ItemDowntime.
• itemDowntime.workOrder – the property on ItemDowntime that references WorkOrder.
That’s all you need; TypeORM will create the proper LEFT JOIN … ON … clauses behind the scenes and the error disappears.
Important points
• Each join path (entity.property) is followed by a unique alias.
• Never swap the arguments: leftJoinAndSelect('relation.path', 'alias').
• Remove duplicate joins (itemTrl.itemDowntimes was listed twice).
With the corrected order and unique aliases the generated SQL is valid and the
WHERE-clause error disappears.