I've made a custom many to many relation between Business and Salesperson, via a joint table BusinessSalesperson. Here is my current code
Business
@OneToMany(() => BusinessSalesperson, (bsp) => bsp.business, {
nullable: false,
onDelete: 'CASCADE',
})
salespersons: Salesperson[]
Salesperson
@OneToMany(() => BusinessSalesperson, (bsp) => bsp.salesperson, {
nullable: false,
onDelete: 'CASCADE',
})
business: Business[]
BusinessSalesperson
@ManyToOne(() => Salesperson, (sp) => sp.business, {
eager: true,
nullable: false,
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'salespersonid' })
salesperson: Salesperson
@ManyToOne(() => Business, (b) => b.salespersons, {
eager: true,
nullable: false,
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'businessid' })
business: Business;
And in my service, I call this
const businesses = this.dataSource.createEntityManager().find(Business, {relations: {salespersons: true, businessSalespersons: { salesperson: true} } });
I try to get the salesperson and businessSalesperson because my salespersons relation was giving me an array of businessSalespersons.
In my results, strangely, I receive hydrated Business, but not my Salesperson
Business {
| businessid: 1365991,
| companyLegalName: 'SCG',
| businessName: 'SCG',
| salespersons: [
| BusinessSalesperson {
| salespersonId: 549,
| businessId: 1365991,
| administrator: false,
| salesperson: null,
| business: [Business]
| }
| ],
| businessSalespersons: [
| BusinessSalesperson {
| salespersonId: 549,
| businessId: 1365991,
| administrator: false,
| salesperson: null,
| business: [Business]
| }
| ]
| }
What am I doing wrong here ?
So, answering my own question if anyone end up in the same case:
I had to add a
referencedColumnName: 'salespersonId'
to my JoinColumn on Salesperson. I guess because of the architecture and naming of the db, TypeOrm was unsure of what to do, and needed more specs.