question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I left-join query with TypeOrm

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem: There are some tables:

CREATE TABLE a (
  a_id serial PRIMARY KEY,
  name text
);

CREATE TABLE b (
  a_id integer REFERENCES a (a_id),
  name text
);

CREATE TABLE c (
  a_id integer REFERENCES a (a_id),
  name text
);

And then I want to query something like this:

SELECT * FROM a
LEFT JOIN b USING (a_id)
LEFT JOIN c USING (a_id)
;

How do I query above with TypeOrm?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11

github_iconTop GitHub Comments

2reactions
Flusinerdcommented, Apr 18, 2020

Try adding this decorator to your a-model

@OneToMany( type =>BDbModel, b => b.a)
b!: BDbModel[];

I think the error is because it cannot find the relation (Only defined from b-side not from the a-side)

Then you should be able to do the following:

.leftJoinAndSelect('a.b', 'b',)

Thats how I do it.

1reaction
resottocommented, Apr 18, 2020

@Flusinerd Thank you for your fast replying. I just tried QueryBuilder with something like this:

import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
import { BDbModel } from './b.entity';

@Entity({ schema: 'public', name: 'a' })
export class ADbModel {
  @PrimaryGeneratedColumn({ name: 'a_id' })
  id!: number;

  @Column({ type: 'text' })
  name!: string;

  b!: BDbModel;
}
import { Entity, ManyToOne, JoinColumn, Column } from 'typeorm';
import { ADbModel } from './a.entity';

@Entity({ schema: 'public', name: 'b' })
export class BDbModel {
  @ManyToOne(() => ADbModel, { primary: true })
  @JoinColumn({ name: 'a_id' })
  a!: ADbModel;

  @Column({ type: 'text' })
  name!: string;
}
  const aDbModel = await conn
    .getRepository(ADbModel)
    .createQueryBuilder('a')
    .leftJoinAndSelect('b', 'b', 'b.a_id = a.a_id')
    .where('a.id = :id', { id: 1 })
    .getRawOne();
  // .getOne();

When I used .getOne(), I couldn’t get the value of b.name in return value. When I used .getRawOne(), I could get the value in return value, but type of return value was just object, not ADbModel (entity of a).

So could I get the value of b.name with ADbModel (entity type)?

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeORM basic join explanation - Stack Overflow
The leftJoinAndSelect("user.photos", "photo") references the property photos defined in the User entity. It's the last line in the User class.
Read more >
typeorm.SelectQueryBuilder.leftJoin JavaScript and ... - Tabnine
How to use. leftJoin. function. in. SelectQueryBuilder. Best JavaScript code snippets using typeorm.SelectQueryBuilder.
Read more >
TypeORM - Query Builder - Tutorialspoint
TypeORM - Query Builder, Query builder is used build complex SQL queries in an easy way. ... Let us perform simple left join...
Read more >
Joins and Queries with Different ORM Tools
SQL Joins using ORM and Query Builders SQL JOIN simple definition from (w3schoo.com) A... Tagged with javascript, node, typeorm, ...
Read more >
TypeORM - Amazing ORM for TypeScript and JavaScript (ES7 ...
Select using Query Builder · Insert using Query Builder · Update using Query Builder · Delete using Query Builder · Working with Relations...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found