aatif shaikh
aatif shaikh

Reputation: 893

how to get a parent ID in child entity object in typeorm + nestjs

I have a simple relation company has many projects and everything is super fine, when I get the result parent ID is missing from entity object while its present in query

Company Entity :

@Entity()
export class Company {
    @PrimaryGeneratedColumn({ unsigned: true })
    id: number;

    @Column({ nullable: false })
    name: string;

    @Column({
        type: 'datetime',
        nullable: false,
        default: () => 'CURRENT_TIMESTAMP',
    })
    created_at;

    @Column({ nullable: true, type: 'datetime' })
    updated_at;

    @OneToMany(
        type => Project,
        project => project.company
    )
    project: Project[];

}

Project Entity :

@Entity()

export class Project {
    @PrimaryGeneratedColumn({ unsigned: true })
    id: number;


    @Column({ nullable: false })
    name: string;

    @ManyToOne(
        type => Company,
        company => company.project,
        { nullable: false }
    )
    @JoinColumn({ name: 'company_id' })
    company: Company;


    @Column({
        type: 'datetime',
        nullable: false,
        default: () => 'CURRENT_TIMESTAMP',
    })
    created_at;

    @Column({ nullable: true, type: 'datetime' })
    updated_at;


}

Query in service :

return this.companyRepository.find({ where: { id: 1 }, relations: ['project'] })

Query log is :

SELECT 
    `Company`.`id` AS `Company_id`,
    `Company`.`name` AS `Company_name`,
    `Company`.`created_at` AS `Company_created_at`,
    `Company`.`updated_at` AS `Company_updated_at`,
    `Company__project`.`id` AS `Company__project_id`,
    `Company__project`.`name` AS `Company__project_name`,
    `Company__project`.`created_at` AS `Company__project_created_at`,
    `Company__project`.`updated_at` AS `Company__project_updated_at`,
    `Company__project`.`company_id` AS `Company__project_company_id`
FROM
    `company` `Company`
        LEFT JOIN
    `project` `Company__project` ON `Company__project`.`company_id` = `Company`.`id`
WHERE
    `Company`.`id` = 1;

but when i print result my company object contains project array but in project array company_id is missing or typeorm has excluded it while creating an object, is there any way than i can get parent_id which is company_id in project object

Result :

Company {
  id: 1,
  name: 'company',
  created_at: 2020-10-24T18:46:59.000Z,
  updated_at: null,
  project: [
    Project {
      id: 1,
      name: 'project 1',
      created_at: 2020-10-24T18:47:35.000Z,
      updated_at: null
    },
    Project {
      id: 2,
      name: 'project 2',
      created_at: 2020-10-24T19:08:08.000Z,
      updated_at: null
    }
  ]
}

expected result :

Company {
  id: 1,
  name: 'company',
  created_at: 2020-10-24T18:46:59.000Z,
  updated_at: null,
  project: [
    Project {
      id: 1,
      company_id: 1,                    <========= this is required
      name: 'project 1',
      created_at: 2020-10-24T18:47:35.000Z,
      updated_at: null
    },
    Project {
      id: 2,
      company_id: 1,
      name: 'project 1',
      created_at: 2020-10-24T19:08:08.000Z,
      updated_at: null
    }
  ]
}

Upvotes: 0

Views: 6417

Answers (2)

Zammy
Zammy

Reputation: 573

I think you should be able to split that up like this. companyId will automatically be extracted from company.

// ...

@ManyToOne(type => Company, company => company.project)
company: Company;

@Column({nullable: true})
companyId: number;

// ...

Upvotes: 0

adamC
adamC

Reputation: 265

In the fireld 'company_id' you are not supposed to get the id field of company, as it is the company object itself. So from that object you may get its id. I think that if you would set 'eager: true' on the field, you will get the object in the query, because the default is eager: false.

If what you want is only the id and not the whole object, maube think of a field which is a foreign key to the company id using one-to-one relation, which will give you your wanted reault output

Upvotes: 1

Related Questions