objection-find
objection-find copied to clipboard
filtering through a HasMany relationship -> "owner.buildFindQuery is not a function"
hi,
i'm trying to do something very similar to an example in the docs:
// This checks if the relation `children` contains at least
// one person whose age is less than 10.
'children.age:lt': 10,
...
i'm getting an error that looks like this:
TypeError: owner.buildFindQuery is not a function
at HasManyRelation.findQuery (/app/backend/node_modules/objection/lib/relations/Relation.js:118:11)
at PropertyRef.buildFilter (/app/backend/node_modules/objection-find/lib/PropertyRef.js:145:28)
...
i dug into the code for both objection-find
and objection
and one of the function signatures doesn't seem to be consistent.
here's where the error is in objection-find
:
const subQuery = rel.findQuery(rel.relatedModelClass.query(), {
ownerIds: rel.ownerProp.refs(builder)
});
and here's the findQuery
function as defined in objection:
findQuery(builder, owner) {
const relatedRefs = this.relatedProp.refs(builder);
owner.buildFindQuery(builder, this, relatedRefs);
return this.applyModify(builder);
}
it looks like findQuery
should take in a RelationOwner
as its second argument, but we're passing in the relatedRefs instead. i'm not sure how to retrieve the owner
- any help would be appreciated!
here are my query parameters for context:
{
eager: '[account,assignments,users]',
'assignments.userId:eq': 1,
}
I can confirm the issue. Only the "HasManyRelation" relation type seems to be affected. It works with "HasOneRelation", for example.
Hello!
First of all, I love objection-filter and it has been super useful!
I have stumbled on this issue as well. Are there any plans to fix this, or maybe there is someone who knows how to pass RelationOwner to findQuery? I have a lot of has-many and many-to-many relations that uses filtering as showed above, and after i updated objection to latest version, this bug showed up.
I know that this type of filtering works without a problem with [email protected].
I assume you are using Objection 2.0.0? This was a breaking change in major version, and I am yet to figure out how to work around it.
Yep. This broke with release of [email protected]. changelog. There was a bug, that we all used as a feature, that inherits parent query table's name. I was playing with objection-find's code, and the only way how i got this to work was by including objection's RelationOwner class in PropertyRef and creating an instance with relation's owner model class (i think..., maybe related model class). It was super hacky, i did it to find a solution, but this probably isn't it. Thou i tested it with my project's examples and it worked.
I dove in objection's code and didn't find any way to get RelationOwner instance without initializing it.
Give us an update for this, for now i created separate queries where i need this functionality, but it would be nice to combine them. Anyways, thank you for this great library.
This functionality was working in version 1.6.6 and broke when moving to 1.6.9. I have not isolated the commit yet but am working a solution. In the meantime, if you don't need any of the features of the later releases you might try 1.6.6 and see if that fixes your issue.
A quick update that I have traced the issue to this commit https://github.com/Vincit/objection.js/commit/1a191b5f155816a33e35b662c7eb903fceadafb3
This issue has been fixed in master and released with version 2.1 however there hasn't been a release to 1.6 version which is what we need here.
Interestingly updating to Objection 2 and Objection-find 2 i get a different error so that's not an option.
What we need is a version of objection-find, ideally 1.6.6 that has the API patch (calls .toKnexQUery instead of .build in PropertyRef).
Due to the tagging in this project I don't see a clean way to provide that so we may need to encourage the library author to help us out here.
@koskimas any chance you could assist?
Further investigation reveals that [email protected] works with [email protected]. I'm not entirely sure why the version change from 1 to 2 but everything appears to be working ok for me. I think in an effort to limit confusion it might be good to include the fix in 1.6.6 as I mentioned above for those developers who may have something like ^1.6.0 in their package.json files.
Since Objection introduced a BC break in 1.9 this may cause additional confusion in the future.
@steverhoades Have you tried [email protected]?
It should work with Objection 2.
Objection-find had a breaking change not related to Objection 2 support, I believe.
I can try to provide a 1.x hotfix if that is preferred, but that would indeed require some code archeology :)