Search code examples
node.jspostgresqljoinnestjstypeorm

nestjs typeorm not selecting join fields nodejs pgsql


I have entity like this

@Entity("my_entity")
export class MyEntity {
  /**
     * Id autoincreament
     */
  @PrimaryGeneratedColumn()
  id: number;

  @ManyToOne(() => Entity2)
  @JoinColumn({ name: "entity_id" })
  @IsInt()
  @IsNotEmpty()
  entityId: number;

  @ManyToOne(() => UserEntity)
  @JoinColumn({ name: "user_id" })
  @IsInt()
  @IsNotEmpty()
  userId: number;

}

and i am getting data like this

 const query: any = {
      where: {
        entityId: In([1,2,3]),
      },
      select: ["id", "entityId", "userId"]
    };
    const users = await this.myRepository.find(query);
    return { data: users };

but this is returning only

{
  "data": [
    {
      "id": 14
    },
    {
      "id": 15
    }
  ]
}

i need other fields as well, am i missing something?


Solution

  • You should add an option loadRelationIds: true to load relation id.

    So query will be

    const query: any = {
          where: {
          },
          loadRelationIds: true,
        };