docs: update many-to-many docs (#10323)
* docs: update many-to-many docs * added note about version * fix eslint error
This commit is contained in:
@@ -150,6 +150,9 @@ const Order = model.define("order", {
|
||||
id: model.id().primaryKey(),
|
||||
products: model.manyToMany(() => Product, {
|
||||
mappedBy: "orders",
|
||||
pivotTable: "order_product",
|
||||
joinColumn: "order_id",
|
||||
inverseJoinColumn: "product_id"
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -161,16 +164,34 @@ const Product = model.define("product", {
|
||||
})
|
||||
```
|
||||
|
||||
At least one side of the many-to-many relationship must have the `mappedBy` property set in the second object parameter of the `manyToMany` object. Its value is the name of the relationship property in the other data model.
|
||||
The `manyToMany` method accepts two parameters:
|
||||
|
||||
In this example, an order is associated with many products, and a product is associated with many orders.
|
||||
1. A function that returns the associated data model.
|
||||
2. An object of optional configuration. Only one of the data models in the relation can define the `pivotTable`, `joinColumn`, and `inverseJoinColumn` configurations, and it's considered the owner data model. The object can accept the following properties:
|
||||
- `mappedBy`: The name of the relationship property in the other data model. If not set, the property's name is inferred from the associated data model's name.
|
||||
- `pivotTable`: The name of the pivot table created in the database for the many-to-many relation. If not set, the pivot table is inferred by combining the names of the data models' tables in alphabetical order, seperating them by `_`, and pluralizing the last name. For example, `order_products`.
|
||||
- `joinColumn`: The name of the column in the pivot table that points to the owner model's primary key.
|
||||
- `inverseJoinColumn`: The name of the column in the pivot table that points to the owned model's primary key.
|
||||
|
||||
<Note>
|
||||
|
||||
The `pivotTable`, `joinColumn`, and `inverseJoinColumn` property are only available after [Medusa v2.0.7](https://github.com/medusajs/medusa/releases/tag/v2.0.7).
|
||||
|
||||
</Note>
|
||||
|
||||
In this example, an order is associated with many products, and a product is associated with many orders. Since the `pivotTable`, `joinColumn`, and `inverseJoinColumn` configurations are defined on the order, it's considered the owner data model.
|
||||
|
||||
### Many-to-Many Relationship in the Database
|
||||
|
||||
When you generate the migrations of data models that have a many-to-many relationship, the migration adds a new pivot table.
|
||||
When you generate the migrations of data models that have a many-to-many relationship, the migration adds a new pivot table. Its name is either the name you specify in the `pivotTable` configuration, or the inferred name combining the names of the data models' tables in alphabetical order, seperating them by `_`, and pluralizing the last name. For example, `order_products`.
|
||||
|
||||
The pivot table has a column with the name `{data_model}_id` for each of the data model's tables. It also has foreign keys on each of these columns to their respective tables.
|
||||
|
||||
The pivot table has columns with foreign keys pointing to the primary key of the associated tables. The column's name is either:
|
||||
|
||||
- The value of the `joinColumn` configuration for the owner table, and the `inverseJoinColumn` configuration for the owned table;
|
||||
- Or the inferred name `{table_name}_id`.
|
||||
|
||||

|
||||
|
||||
---
|
||||
|
||||
Reference in New Issue
Block a user