-
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
hasMany relation to same Entity fails #1571
Comments
@jdumont0201 This is an interesting problem. What is the particular use case you are trying to solve here? I would expect an error when trying to do this because you are essentially defining a recursive relation here ... Todo -> Todo 1. Todo 1 -> Todo 2. Todo 1 -> Todo 3. Todo 2 -> Todo 4. And now the final chain should be as follows:
This can keep going on forever and ever. My suggestion would be to try a different data model where a Model is extended to create new Models. Define
And now you can establish a relation of ParentTodo hasMany ChildTodo and this shouldn't result in an infinite recursion / stack errors all the while both these Models will rely on the same base model so any changes made to one will reflect to the other. Thoughts? |
There are many models that have fractal properties, i.e. contains entities that have the same properties. Some usecases:
In Loopback 3, I went with hasManyThough, specifying foreignKey and keyThrough for each pair of relation and two belongsTo relations on the through entity. It allows multiple /contained_in /containing relationships I also like your inheritence idea, I'll give it a try and keep you informed |
Thanks for providing a use-case! Let me know how the inheritance idea works out / any issues you run into it, we'll be more than happy to help. As for support for |
Here is the problem I see purely based on the error stack trace given: Even if we fix this issue, I am not sure whether that would still allow the program to work as intended as @virkt25's comment above. IMO, we need to investigate this issue by:
I'd imagine some language problem we have with JS would be solved by this PR, #1618, via the usage of resolvers, but I'm not too sure. |
In my opinion, a recursive Consider an application modeling company employees. Each manager is an I like the plan proposed by @shimks in his comment 👍
@jdumont0201 would you like to take a look at this yourself and contribute a patch or two to fix the issues? I think the following test case is a good starting point - just copy the code into a new test case, modify the model definition to contain a property storing an instance of the same model, and then find out how to make the test pass. @shimks please correct my advice if needed. |
let's do a quick spike:
|
Discussed with @raymondfeng , this task will be postGA but would like the spike to be in for GA. |
There's a similar issue in BelongsTo. I have a model called Category with a parentId field that points to the parent Category
I created a /categories/{id}/parent endpoint per the docs When I call that endpoint I get this error:
Let me know if that's tied into this issue or if a separate issue should be opened for this one. |
@hvlawren could you please post the dependency injection configuration for your |
@hvlawren actually, the problem you have encountered is almost certainly different from what is being discussed in this issue, would you mind opening a new GitHub issue please? |
I created #2118 -- Thanks! |
@nabdelgadir , could you please add the acceptance criteria based on the spike #1682? Thanks. |
Description / Steps to reproduce / Feature proposal
Can we have an hasMany relation between objects of the same Entity ?
Say an object that refers to other objects of the same type.
Apparently it cannot find the target key, or loops endlessly when specified explicitly.
Current Behavior
Start from the TodoList example.
Add an hasMany relation to Todo that links to other Todos.
todo.repository.ts
todo.model.ts
fails with
If I specify the keyTo target in todo.model.ts, then it fails with max call stack exceeded.
Expected Behavior
See Reporting Issues for more tips on writing good issues
Acceptance Criteria
The text was updated successfully, but these errors were encountered: