0
votes

I have three tables and entities:

  • customer
  • shop
  • shop_customer

Relation between those are:

Customer:

@OneToMany((type) => ShopCustomer, (shopCustomer) => shopCustomer.customer, { eager: false })
shopCustomer: ShopCustomer[];

Shop:

@OneToMany((type) => ShopCustomer, (shopCustomer) => shopCustomer.shop, { eager: false })
shopCustomer: ShopCustomer[];

ShopCustomer:

  @ManyToOne((type) => Customer, (customer) => customer.shopCustomer, { eager: false })
  customer: Customer;

  @ManyToOne((type) => Shop, (shop) => shop.shopCustomer, { eager: false })
  shop: Shop;

So the problem is that when I create migrations and run them, I get correct structure for shop_customer table, so:

id, customerId, shopId, Role

(Role is just a string)

The issue is with shop and customer tables. In those I get shopCustomerId columns created. Do you know what should I do in order to make it work?

Thank you for help <3

3
you are trying to create many to many relationship table that contains foreign keys from other two tables? if yes then you should check typeorm.io/#/many-to-many-relations this. I made post and tag relationship and it works fine.Solvenc1no
@Solvenc1no It would work If I didn't need custom field (Role).Szymon Ellwart

3 Answers

1
votes

There was a caching problem. Disabling cache in ORM config file and recrating migrations resolved the issue.

0
votes

It's because you have @OneToMany relations defined on Shop and Customer, so TypeOrm creates a foreign-key column to ShopCustomer.

TypeOrm many-to-one documentation specifically says "If you only care about the @ManyToOne relationship, you can define it without having @OneToMany on the related entity."

Just remove the @OneToMany relations on Shop and Customer.

0
votes

The relations are ok, maybe u done the wrong relations before, try to erase de dist folder in root to erase the entities cache. As said before the @OneToMany are note needed, but is supposed they don't create any field too.

Maybe another solution is doing this code

@OneToMany((type) => ShopCustomer)
shopCustomer: ShopCustomer[];

the inverse relation in @ManyToOne are not necessary, and the eager for default is false.