Skip to content

Find entity with relation returns array of one null entity for M2M relation if none exist. #3163

@rodp82

Description

@rodp82

Issue type:

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

Database system/driver:

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

TypeORM version:

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

I have the following model structure:
Employees have many Skills and Skills can be assigned to many Employees but the joining table has extra columns such as startDate and proficiency.

@Entity({ name: 'employees' })
export class Employee { 
  @Column()
  name: string;

  @OneToMany(type => EmployeeSkill, empSkill => empSkill.employee)
  skills: EmployeeSkill[];
}

@Entity({ name: 'skills' })
export class Skill {

  @Column()
  name: string;

  @OneToMany(type => EmployeeSkill, empSkill => empSkill.skill)
  employees: EmployeeSkill[];
}

@Entity({ name: 'employee_skills' })
export class EmployeeSkill {

  @ManyToOne(type => Employee, employee => employee.skills, { primary: true })
  employee: Employee;

  @ManyToOne(type => Skill, skill=> skill.employees, { primary: true })
  skill: Skill;

  @Column()
  startDate: Date;

  @Column()
  proficiency: string;
}

Then when I call

const employee = await getRepository(Employee).findOne(id, { relations : ['skills']});

I get the following result:

{
    name: "Employees Name",
    skills: []
}

Which is correct. However, when trying to add the relation from EmployeeSkill to Skill, either with { eager: true } in the entity or { relations : ['skills', 'skills.skill'] } in the findOne() or find() options, I get:

{
    name: "Employees Name",
    skills: [
        {
            skill : null,
            startDate : null,
            proficiency : null,
        }
    ]
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions