Skip to content
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

filtering through a HasMany relationship -> "owner.buildFindQuery is not a function" #53

Open
leearaneta opened this issue Jan 24, 2020 · 10 comments

Comments

@leearaneta
Copy link
Contributor

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,
}
@devland
Copy link

devland commented Mar 12, 2020

I can confirm the issue. Only the "HasManyRelation" relation type seems to be affected. It works with "HasOneRelation", for example.

@thisiskalnins
Copy link
Contributor

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 objection@1.5.3.

@kibertoad
Copy link
Collaborator

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.

@thisiskalnins
Copy link
Contributor

Yep. This broke with release of objection@1.6.0. 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.

@steverhoades
Copy link

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.

@steverhoades
Copy link

A quick update that I have traced the issue to this commit Vincit/objection.js@1a191b5

@steverhoades
Copy link

steverhoades commented May 19, 2020

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?

@steverhoades
Copy link

steverhoades commented May 19, 2020

Further investigation reveals that objection-find@2.1 works with objection@1.6.11. 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.

@kibertoad
Copy link
Collaborator

kibertoad commented Nov 19, 2020

@steverhoades Have you tried objection-find@3.x? It should work with Objection 2.
Objection-find had a breaking change not related to Objection 2 support, I believe.

@kibertoad
Copy link
Collaborator

I can try to provide a 1.x hotfix if that is preferred, but that would indeed require some code archeology :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants