25

I tried to select specific columns by joining tables in typeorm.

When I see following materials there is sample code.

https://orkhan.gitbook.io/typeorm/docs/select-query-builder#joining-relations

const user = await createQueryBuilder("user")
    .leftJoinAndSelect("user.photos", "photo")
    .where("user.name = :name", { name: "Timber" })
    .getOne();
import {Entity, PrimaryGeneratedColumn, Column, OneToMany} from "typeorm";
import {Photo} from "./Photo";

@Entity()
export class User {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    name: string;

    @OneToMany(type => Photo, photo => photo.user)
    photos: Photo[];
}
import {Entity, PrimaryGeneratedColumn, Column, ManyToOne} from "typeorm";
import {User} from "./User";

@Entity()
export class Photo {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    url: string;

    @ManyToOne(type => User, user => user.photos)
    user: User;
}

for example my desired result is following.where user.name =="Timber"

{
id: user.id
name: user.name
url: photo.url
}

Are there any good way to achieve this ?

Thanks

2 Answers 2

35
const user = await createQueryBuilder("user")
    .leftJoinAndSelect("user.photos", "photo")
    .select(['user.id', 'user.name', 'photo.url']) // added selection
    .where("user.name = :name", { name: "Timber" })
    .getOne();

By this query, you'll get:

{
  id: 1,
  name: 'Timber',
  photos: [{ url: 'someurl1' }, ..., { url: 'someurlN' }]
}
Sign up to request clarification or add additional context in comments.

4 Comments

How to assign alias for those selected columns? I tried .select(['user.id', 'user.name', 'photo.url'],['userId','username','url']), but this approach did not work
@AkhilMohandas try .select(['user.id AS userId', 'user.name AS username', 'photo.url AS url'])
@ArtOlshansky I have tried 'delivery.delivery_type AS type' with getOne. unable to get alias. its working with getRawOne().but I need getOne(). stackoverflow.com/q/72901411/4909563
Make sure to not put the aliases in quotation marks. E.g. this doesn't work: .select(["user"."id", "user"."name"]). Doing this only works with getRawOne(). For the mapping to work, the aliases must not be in quotes.
9

When you want to select particular columns you have to use getRawOne like below,

const user = await createQueryBuilder("user")
    .leftJoinAndSelect("user.photos", "photo")
    .select(['user.id', 'user.name', 'photo.url']) 
    .where("user.name = :name", { name: "Timber" })
    .getRawOne();    

1 Comment

Just wanna complete this answers: In my experience, it does not matter if you wanna use getRawMany, getRawOne, or execute, or other methods. You need to keep in mind that for some reason you need to chain your select at the end of the methods -- createQueryBuilder("tb").leftJoinAndSelect("tb.tb2", "tb2").otherMethods().select([/* fields */]).getOne();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.