question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Filtering Included Relationships not working with IQuery "where" method

See original GitHub issue

The documentation mentions the following:

Filtering Included Relationships
Similarly, you may filter included relationships using where(), and(), and or().

this._userRepository
    .include(u => u.posts)
    .where(p => p.archived)

This doesn’t seem to work with “where”, the inferred type is User, but it works with and/or and post is inferred.

I have been using “and” as a workaround in most cases and it works fine. Not knowing the internals of the code (or tested in depth) the concerns I have are:

  • could it be causing other undesirable side effects or is it just the typings?
  • a little bit of readability of the query is lost, which I think would be valuable since there is already some level of implicit nesting in the library.
  • In case it’s hard to fix, it would be cool if the docs point out the limitation

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
IRCraziestTaxicommented, Mar 16, 2019

I updated the documentation (version 1.0.0-alpha.15) and am going to go ahead and close this issue. I will try to figure something out if somebody opens it back up.

1reaction
IRCraziestTaxicommented, Mar 14, 2019

Ah… I see what’s going on. The Query class allows for it, but the interface does not.

My intention (some time after the time at which the initial documentation was written) was for include to not behave like a join when using where.

My thought was this:

userRepository
    .getMany()
    // I want to include posts in my results.
    .include(u => u.posts)
    // However, I am filtering on the user itself.
    .where(u => u.active)
    .isTrue();

userRepository
    .getMany()
    // Filter results by users who have archived posts.
    // Hence "join where posts are archived".
    .join(u => u.posts)
    .where(p => p.archived)
    .isTrue()
    // Now that "join where" is over, "where" operates on the user again.
    .where(u => u.active)
    .isTrue();

I think the reason I did that was because “include where” didn’t make a lot of sense to me in the sense that doing it the same way as “join where” would yield unexpected results - i.e. “include where” would not give you all users with filtered posts as you might expect, but rather would filter your user results like a “join where” would.

Obviously, given the way it works now, the documentation is extremely misleading and I will update it. I also wonder if “include where” should be allowed, but rather than doing an inner join as “include where” does, it would do a left join to produce the expected results discussed above.

The problem that presents, however, is that would break the way include().where() works now, so it would probably be best to not change the behavior, but rather just update the documentation.

I guess the workaround that will be explained in the updated documentation is to use .include() followed by .join().where() afterwards:

userRepository
    .getMany()
    // Include posts in results.
    .include(u => u.posts)
    // Use .join rather than .joinAlso to actually filter user results by post criteria.
    .join(u => u.posts)
    .where(p => p.archived)
    .isTrue();

userRepository
    .getMany()
    // Include posts in results.
    .include(u => u.posts)
    // Use .joinAlso rather than .join to perform a left join to filter posts but not filter users.
    .joinAlso(u => u.posts)
    .where(p => p.archived)
    .isTrue();

Hopefully, if and when I finally get around to making this work, these weird quirks will be made obsolete as more context can be deduced when parsing the where function.

As a side note, I am surprised that using and without a where before it did not error or produce unexpected results. I may need to check into that as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Filter functions do not work on relationships #12725 - GitHub
Any functions applied in the filter on relationships fail to execute correctly. This is seems to be both an app issue and api...
Read more >
Laravel eloquent where filter HasMany relationship not ...
First at all with function of eloquent it's look like as LEFT JOIN on MYSQL so if you want only your teams where...
Read more >
Solved: Relationship filtering on many to one doesnt filte...
Solved: Hi, I have several tables all linked to each other. What I am finding is that the filtering from many to one...
Read more >
Relationship troubleshooting guidance - Power BI
BLANK groupings or slicer/filter items appear, and the source columns don't contain BLANKs, - It's a regular relationship, and "many"-side column contain values ......
Read more >
Filtering Results Using Filtered Include Method in EF Core
In this article, we are going to show you how to use Filtered Include method in EF Core to filter results inside the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found