fastapi-filter icon indicating copy to clipboard operation
fastapi-filter copied to clipboard

Automatically join if filtering by relationship field

Open Kastakin opened this issue 2 years ago • 3 comments

This aims to be a first tentative approach at avoiding the need for manual joining when filtering a table.

Before the filtering itself relationship of the models are retrieved. If one of the filtering field is associated with a relationship and it contains values not only null values the corresponding table is joined. This accommodates for nested filters and tables connected through a secondary table.

I call this change tentative for three main reasons:

  1. I am not sure this is something of only personal interest or if there is a need for it
  2. I do not have experience working with MongoDB through mongoengine, as such I still have not touched the code relative to it.
  3. Some tests are failing due to warning to cartesian products between columns. In my use case this does not seem to generate wrong results, but I am not 100% sure on the best course of action could be in this case to avoid the warning altogether, as such I have not changed the tests yet to pass. Some are still failing due to the aforementioned warning.

Kastakin avatar Jan 29 '24 13:01 Kastakin

Deploy Preview for fastapi-filter ready!

Name Link
Latest commit 658fe9809c25ff8a22a5fcbdb2b1dc9d352ae39e
Latest deploy log https://app.netlify.com/sites/fastapi-filter/deploys/6613e726e8776100080dda54
Deploy Preview https://deploy-preview-550--fastapi-filter.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

netlify[bot] avatar Jan 29 '24 13:01 netlify[bot]

Codecov Report

Attention: Patch coverage is 85.71429% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 98.17%. Comparing base (e0f2fa8) to head (658fe98).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##              main     #550      +/-   ##
===========================================
- Coverage   100.00%   98.17%   -1.83%     
===========================================
  Files            6        6              
  Lines          193      219      +26     
===========================================
+ Hits           193      215      +22     
- Misses           0        4       +4     
Files Coverage Δ
fastapi_filter/contrib/sqlalchemy/filter.py 94.87% <85.71%> (-5.13%) :arrow_down:

codecov[bot] avatar Feb 02 '24 11:02 codecov[bot]

I have looked a bit more into what was causing the tests to fail and reworked everything to pass it.

Nested behavior was not tested yet. I actually think it is possible to modify what it is already in place slightly to test nested filters too (e.g., the city column could become another table connected to the address one).

In my application I am already filtering in both nested and many-to-many relationships without any evidence of problems.

I can commit to the task more if it is something actually of interest.

Kastakin avatar Feb 02 '24 11:02 Kastakin