objection.js: Bug: 0.9 upsertGraph HasMany no insert in one particular case
It seems to be a bug in 0.9 with upsertGraph in the following case:
Model: grandparent (update) (BelongsToOne parent)-> parent (update) (HasMany child)-> child(insert).
In this case in the dependency graph, the parent get markAsHandled set after established that it has a dependency to grandparent. After this, however, is determined that child also has dependency to parent but markAsHandled for parent is already set.
This leads to hasUnresolvedDependencies failing for child with numHandledNeeds = 0 and needs = 1. This leads to no insert.
markAsHandled should probably be called after all dependencies have been resolved. As a workaround I set manually in DependencyGraph:
if (rel) {
if (rel instanceof HasManyRelation) {
node.needs.push(new HasManyDependency(parentNode, rel));
parentNode.isNeededBy.push(new HasManyDependency(node, rel));
// new line
node.numHandledNeeds++;
This will of course only work in my particular case.
Could maybe be useful with an error/warning message for unresolved dependencies? Many thanks.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 21
Thank you! Now there’s a reproduction I can work with 👍
This is not actually a bug, but may be a missing feature. It depends on what you are trying to achieve with this. Are you trying to say:
Relate if id is found in the database, otherwise insert a new row.
?If that’s what you want, unfortunately it’s not supported at the moment. What
insertMissing
means isIf parent doesn't have a child by the given id, insert a new item.
. It doesn’t check if the id already exists in the database. Implementing this would be all kinds of difficult too, since we would need to do a bunch of new queries to find out if the given item exist in the database. Not to mention what to do to relations that are found, but their children are not… My brains explode just thinking about implementing this.If you are trying to simply insert the ones with ids, you can just remove the
relate
option: