7
votes

Given the following two TypeORM entities that have a @ManyToMany relationship:

@Entity({ name: 'products' })
export class ProductEntity {
  @PrimaryColumn()
  id: number;

  @Column()
  name: string;

  @ManyToMany(type => CategoryEntity, { eager: true })
  @JoinTable({ name: 'products_categories' })
  categories: CategoryEntity[];
}
@Entity({ name: 'categories' })
export class CategoryEntity {
  @PrimaryColumn({ length: 40 })
  code: string;
}

As a result I get created a table called "products_categories" with the following column names:

  • productsId
  • categoriesCode

Is there a way of giving these two columns custom names? I would like to rename them as follows:

  • productsId -> productId
  • categoriesCode -> categoryCode
1

1 Answers

17
votes

Solved it by extending the options object given to @JoinTable, which I missed from the TypeORM docs:

@ManyToMany(type => CategoryEntity, { eager: true })
@JoinTable({
    name: "products_categories",
    joinColumn: {
        name: "product",
        referencedColumnName: "id"
    },
    inverseJoinColumn: {
        name: "category",
        referencedColumnName: "id"
    }
})
categories: CategoryEntity[];