Querying nested relations many to many with TypeORM

I have three tables, products, items, and parts. The relation between them is ManyToMany, but at Typeorm level, I decided to use ManyToOne and OneToMany because I want to use the intermediate table as an entity.

products

import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn, OneToMany } from 'typeorm';
import { Item } from 'src/items/item.entity';


@Entity({ name: 'products' })
export class Product {
  @PrimaryGeneratedColumn('increment')
  id: number;

  @Column({ length: 100 })
  code: string;

  @Column({ length: 255 })
  description: string;

  @Column({ length: 255 })
  filename: string;

  @Column({ default: false, nullable: false, name: 'is_deleted' })
  isDeleted: boolean;

  @CreateDateColumn({ type: 'timestamp', nullable: false, default: () => 'CURRENT_TIMESTAMP', name: 'creation_date' })
  createdAt: Date;

  @UpdateDateColumn({ type: 'timestamp', nullable: false, default: () => 'CURRENT_TIMESTAMP', name: 'last_modification_date' })
  updatedAt: Date;

  @OneToMany(() => Item, item => item.id)
  items: Item[];
}

items

import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn, JoinColumn, ManyToOne } from 'typeorm';
import { Part } from 'src/parts/part.entity';
import { Product } from 'src/products/product.entity';

@Entity({ name: 'items' })
export class Item {
  @PrimaryGeneratedColumn('increment')
  id: number;

  @ManyToOne(() => Part, part => part.id)
  @JoinColumn({ name: "part_id", referencedColumnName: "id"})
  part: Part;

  @ManyToOne(() => Product, product => product.id)
  @JoinColumn({ name: "product_id", referencedColumnName: "id"})
  product: Product;

  @Column()
  quantity: number;

  @Column({ default: false, nullable: false, name: 'is_deleted' })
  isDeleted: boolean;

  @CreateDateColumn({ type: 'timestamp', nullable: false, default: () => 'CURRENT_TIMESTAMP', name: 'creation_date' })
  createdAt: Date;

  @UpdateDateColumn({ type: 'timestamp', nullable: false, default: () => 'CURRENT_TIMESTAMP', name: 'last_modification_date' })
  updatedAt: Date;
}

parts

import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn, ManyToOne, JoinColumn, OneToMany } from "typeorm";
import { Subvehicle } from "src/subvehicles/subvehicle.entity";
import { Item } from "src/items/item.entity";

@Entity({ name: 'parts'})
export class Part {
  @PrimaryGeneratedColumn('increment')
  id: number;

  @Column({ length: 255 })
  oem: string;

  @Column({ default: false, nullable: false, name: 'is_deleted' })
  isDeleted: boolean;

  @CreateDateColumn({ type: 'timestamp', nullable: false, default: () => 'CURRENT_TIMESTAMP', name: 'creation_date' })
  createdAt: Date;

  @UpdateDateColumn({ type: 'timestamp', nullable: false, default: () => 'CURRENT_TIMESTAMP', name: 'last_modification_date' })
  updatedAt: Date;
  
  @OneToMany(() => Item, item => item.id)
  items: Item[];
}

The issue is when I try to find the nested entities from the product

@Injectable()
export class ProductsService {
  constructor(@InjectRepository(Product) private productRepository: Repository<Product>) { }

  findAll(): Promise<Product[]> {
    return this.productRepository.find({ relations: ["items"] });
  }
}

I have been trying to fit this, but not sure why I am getting the error

[Nest] 43784 – 09/21/2023, 10:58:25 AM ERROR [ExceptionsHandler]
Cannot read properties of undefined (reading ‘joinColumns’) TypeError:
Cannot read properties of undefined (reading ‘joinColumns’)

  • 1

    Please don’t tag your titles. See How to Ask.

    – 

Leave a Comment