TypeORM joins by multiple PrimaryColumns ignoring defined JoinColumn

34 views Asked by At

I have an entity called Pack with two PrimaryColumns - idpack and idproduct:

import {
  CreateDateColumn,
  Entity,
  JoinColumn,
  OneToMany,
  PrimaryColumn,
  PrimaryGeneratedColumn,
} from 'typeorm';

import { productospack } from './productospack.entity';

@Entity({
  schema: 'sch_main',
  name: 'packs',
})
export class packs {
  @PrimaryGeneratedColumn()
  idpack: number;

  @PrimaryColumn('int')
  idproducto: number;

  @CreateDateColumn()
  creationdate: Date;

  @OneToMany(() => productospack, (productpack) => productpack.pack)
  @JoinColumn({ name: 'idpack' })
  productos: productospack[];
}

This entity has a OneToMany relation to another entity called PackProduct:

import {
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { packs } from './pack.entity';

@Entity({
  schema: 'sch_main',
  name: 'productospack',
})
export class productospack {
  @PrimaryGeneratedColumn()
  idproductopack: number;

  @Column('int')
  idpack: number;

  @Column('int')
  idproducto: number;

  @Column('int')
  productquantity: number;

  @ManyToOne(() => packs, (pack) => pack.productos)
  @JoinColumn({ name: 'idpack' })
  pack: packs;
}

I expected that when querying for a Pack, TypeORM would join the PackProduct table by idpack only, since I have defined @JoinColumn({name: 'idpack'}).

However, when executing the query, I see that TypeORM is joining on both idpack and idproduct columns:

INNER JOIN PackProduct 
ON PackProduct.idpack = Pack.idpack 
AND PackProduct.idpack = Pack.idproduct

This results in an invalid join, since I need it to join only by idpack.

I would expect TypeORM to respect the defined @JoinColumn and join only on that column, instead of joining on all PrimaryColumns.

I am using TypeORM version ^0.3.19 with PostgreSQL database.

I have tried explicitly defining the join column in the inverse side of the relationship as well but the behavior is the same.

Is this expected behavior when having multiple PrimaryColumns?

Or could it be considered a bug?

How can I make it join only on the defined @JoinColumn in this case?

0

There are 0 answers