-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
belongsToMany relation to model of the same type fails #2118
Comments
In your CategoryRepository, you are injecting a getter for obtaining the same repository again. Since your relation is referring back to the same model, you can reuse the same repository. Could you please check if the following code fixes to problem for you? export class CategoryRepository extends DefaultCrudRepository<
Category,
typeof Category.prototype.id
> {
public readonly container: BelongsToAccessor<Domain, number>;
public readonly parent: BelongsToAccessor<Category, number>;
constructor(
@inject('datasources.db') dataSource: DbDataSource,
@repository.getter(DomainRepository)
protected domainRepositoryGetter: Getter<DomainRepository>,
) {
super(Category, dataSource);
this.container = this._createBelongsToAccessorFor('container', domainRepositoryGetter);
this.parent = this._createBelongsToAccessorFor('parent', Getter.fromValue(this));
}
} I think you won't be the last person encountering this issue, let's describe the problem & the solution in our docs, e.g. hasMany relation and belongsTo relation. (Feel free to propose other places where you would be looking for this information.) Would you @hvlawren like to contribute this documentation change yourself? |
That fixed it! I'd say those two places probably make the most sense. I've got a lot on my plate right now, so I'd suggest someone else add the docs. |
Great! 🎉
We all have a lot on our plates, don't we? 😉 It would be great if you could find some time later to contribute back, but I will understand if you don't. Let's keep this issue open, I think it's a good opportunity for new contributors. |
Well, I am sniffing around for good first issues. Will take this up. |
Hello. |
I just realized that if the above CategoryRepository has a hasMany relation to DocumentRepository and DocumentRepository has a belongsTo relation to CategoryRepository then you get a circular dependency error. |
@mmeilby: do you found a solution for the case |
Yes, the solution in May was to add this line before the repository class definition: |
From discussion on #1571
Description
I have a model called Category with a parentId field that points to the parent Category
Category Model:
I created a /categories/{id}/parent endpoint per the docs
When I call that endpoint I get this error:
Category Parent Controller:
Category Repository
Note that the container part works as expected, only parent is broken.
The text was updated successfully, but these errors were encountered: