I have two entities Order
and Address
and they have @ManyToMany
relationship .
Address entity :
@ManyToMany(() => Order, (order) => order.address, { cascade: true })
@JoinTable()
order: Order;
Order entity :
@ManyToMany(() => Address, (address) => address.order, { cascade: true })
address: Address;
I need to know which entitiy need the cascade option and how can I identify that ?
Should I just use the cascade option in both entities ?
When cascade is set to true
on a relationship, lets say in the Order entity. Every time you attach an Address to an Order. You don't have to save the address separately, it will automatically be handled by Typeorm and will be inserted in the database accordingly. You only need to save the Order.
example:
const order = new Order();
order.address = address;
await order.save();
So with that, with this context. I would say, put it in the Order
entity if you are attaching an address to an order and NOT vice versa, else, your setup is fine