Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

287
Visualizações
get fields instead of object from relation (typeorm)

I am new to typeorm and when i want to retrieve a table that has a relation, the columns of that relation are retrieved in an object with the name of that table. Here is an example

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
}
const user = await createQueryBuilder("user")
    .leftJoinAndSelect("user.photos", "photo")
    .where("user.name = :name", { name: "Timber" })
    .getOne()

Result

{
    id: 1,
    name: "Timber",
    photos: [{
        id: 1,
        url: "me-with-chakram.jpg"
    }, {
        id: 2,
        url: "me-with-trees.jpg"
    }]
}

I want to get the fields of the relation at the same level as the fields of my table

Expected Result

{
   {
    id: 1,
    name: "Timber",
    id: 1,
    url: "me-with-chakram.jpg"
    }
   {
    id: 1,
    name: "Timber",
    id: 2,
    url: "me-with-trees.jpg"
    }
}

Is there an option to do this ? I didn't find anything in their docs

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can try this,

const user = await createQueryBuilder("user")
    .leftJoinAndSelect("user.photos", "photo")
    .select([
            'user.id',
            'user.name',
            'photos.id',
            'photos.url'
        ])
    .where("user.name = :name", { name: "Timber" })
    .getOne();
about 4 years ago · Juan Pablo Isaza Relatório

0

I tried using '*' and it worked

  const user = await createQueryBuilder("user")
        .leftJoinAndSelect("user.photos", "photo")
        .select('*')
        .where("user.name = :name", { name: "Timber" })
        .getOne();
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda