nestjs-paginate icon indicating copy to clipboard operation
nestjs-paginate copied to clipboard

Is there a way to include null values when filtering on relations ?

Open jlefebvre1997 opened this issue 1 year ago • 2 comments

At the moment, since filtering on relations act as a simplified left join, if the requested relation is null, the results are not included. I think it's not the right behaviour, since for example if I ask for cats filtering by toys.color=$not:$eq:red, I expect cats that do not have any toys to show up as well (but that's debatable ofc)

jlefebvre1997 avatar Nov 20 '24 14:11 jlefebvre1997

Left joins should include null values 🤔 Inner joins don't. Can you see if adding a 2nd filter with toys.color=$or:$null gets you the correct results?

Helveg avatar Nov 26 '24 07:11 Helveg

@jlefebvre1997 have you had a chance to look into the added null filter? If you have the time, could you open a PR where you add a failing test case to demonstrate the problem? Then I can look into a fix :)

Helveg avatar Mar 06 '25 12:03 Helveg

Hey @Helveg,

I think I have a similar problem or the same. I'm having a OneToOne relationship between the entities, so the field can be null, or contain the joined entity.

When filtering with no filter: Result: 21 total items (All in my DB)

When filtering with:

    "filter": {
      "occurrence.status.label": "$not:$eq:fixed"
    }

Result: 1 total item So only Occurrence Entities are include which have status not null and not fixed

When filtering with following filter as suggested in the comments earlier:

    "filter": {
      "occurrence.status.label": [
        "$not:$eq:fixed",
        "$or:$null"
      ]
    }

Result: 1 total item So the entities with status = null are still not included

Any idea why this is not correct or did I use the filter wrong?

dariozachow avatar Sep 22 '25 10:09 dariozachow

had to set the joinMethods to left join. Now it works for me

dariozachow avatar Sep 22 '25 12:09 dariozachow