-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for self relationships (#10071)
Initially I thought that we will have to add special checks to allow relationships referencing itself. However, it turned out that not to be the case. Instead, it had more to do with how self relationships are defined in Mikro ORM. In case of `belongsTo` relationship we have to define the other side as well either as a `hasMany` or `hasOne`. For example: **❌ The following code will fail, because no children are defined for the parent** ```ts const user = model.define("user", { id: model.number(), username: model.text(), parent: model.belongsTo(() => user) }) ``` **✅ Addition of children relationship will make things work** ```ts const user = model.define("user", { id: model.number(), username: model.text(), parent: model.belongsTo(() => user, { mappedBy: "children" }), children: model.hasMany(() => user, { mappedBy: "parent" }), }) ``` We can see the similar setup here with our `ProductCategory` MikroORM entity. https://github.com/medusajs/medusa/blob/develop/packages/modules/product/src/models/product-category.ts#L87-L94 @adrien2p Correct me if I am wrong. But I have added the tests for now so that we know the behavior of self relationships
- Loading branch information
1 parent
0ea5765
commit 98bf8c9
Showing
1 changed file
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters