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
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[];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With