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

_and filter doesn't work as expected #21169

Open
fatihsolhan opened this issue Jan 23, 2024 · 9 comments
Open

_and filter doesn't work as expected #21169

fatihsolhan opened this issue Jan 23, 2024 · 9 comments

Comments

@fatihsolhan
Copy link

Describe the Bug

Hi, I'll try my best to explain it with screenshots.

As you can see on the first query, even though I wrapped entire filter with _and, it still returns a response when it shouldn't since there is a _neq filter for date 2023-12-122
image

Here is another approach for this query but still returns a data
image

Lastly, I've tried to narrow the _and filter but I got an error this time
image

To Reproduce

Just make a search for a date field with _and filter

Directus Version

v10.8.2

Hosting Strategy

Self-Hosted (Docker Image)

@br41nslug
Copy link
Member

Instead of _and/_neq (which needs to be at the root level, https://docs.directus.io/reference/filter-rules.html#logical-operators) i think you're looking for _none or _some to filter relational data https://docs.directus.io/reference/filter-rules.html#some-vs-none-in-one-to-many

@fatihsolhan
Copy link
Author

fatihsolhan commented Jan 23, 2024

Instead of _and/_neq (which needs to be at the root level, https://docs.directus.io/reference/filter-rules.html#logical-operators) i think you're looking for _none or _some to filter relational data https://docs.directus.io/reference/filter-rules.html#some-vs-none-in-one-to-many

Thanks for quick reply! I've put the _and filter to the root in the first screenshot, is it not correct?

Also, I saw _none and _some filters but unfortunately they don't exist for M2O relation.
Screenshot 2024-01-23 at 17 06 24

@br41nslug
Copy link
Member

br41nslug commented Jan 23, 2024

Judging from the returned results you're working with an many-to-many not a one-to-many right? (else there would be nothing to filter because an m2o can only have 1 item related to it). Oooh i see are you trying to filter inside the relation? if so you'll want to use a deep filter probably.

@fatihsolhan
Copy link
Author

fatihsolhan commented Jan 23, 2024

Yeah, I am trying to filter inside the relation. Since deep is natively supported with graphql queries, I don't think I need it, right? The structure is like this:

Rentals

  • calendar: M2O to Calendars 👇🏻

Calendars

  • calendarDay: M2M to CalendarDays 👇🏻

CalendarDays

  • id
  • date

@br41nslug
Copy link
Member

You're very right indeed but graphql does requires it's own nested filters for that https://docs.directus.io/reference/query.html#examples-6

notice the filter on the relation not on the root item
image

@fatihsolhan
Copy link
Author

the approach makes very much sense, thank you for that.

I've tried this simple _neq but it didn't work as I expected.
Screenshot 2024-01-23 at 17 42 22

Then I tried this and and I think I understand the issue
Screenshot 2024-01-23 at 17 44 53

In the first case, it searchs for _neq, finds the 2023-12-26 record and returns that calendar entity even though I have the calendar has a record with 2023-12-22

I think my approach for this could be wrong from the start. I want to get the rentals that doesn't have calendar.calendarDay record with dates I search

@fatihsolhan
Copy link
Author

hey @br41nslug
I've simplified my collections and attached calendarDays to the rentals with M2M.

Here is my latest query and it doesn't work. I think it's not about _and filter anymore. I should be able to filter the rentals based on calendarDays value, that's why I've added filter to the rentals, not to the calendarDays

Screenshot 2024-01-26 at 11 32 51

@br41nslug
Copy link
Member

br41nslug commented Jan 26, 2024

The result looks correct (even tho counter intuitive at first). This is where the previously mentioned _some/_none come in. _some is the default behavior for relational filters so your filter returns "calendar days that have _some calendarDays with a date _not_in "2024-01-01". Judging from your query you want this to behave like _none + _in.

Not sure if you can currently achieve this with graphql #14228

@fatihsolhan
Copy link
Author

Thank you for your help @br41nslug. Knowing that _some is the default behavior for relational filters made things more clear to me. I looked to the other issues and looks like the team working on a new tool called Durus which is I'm assuming (and hoping 🙏 ) gonna fix these issues.

I've tried another approach for this but it still didn't work as expected
Screenshot 2024-01-27 at 19 51 28

Directus is a great tool and helped to me on lots of projects, I can't thank you guys enough for this awesome tool, but I have to say I'm very surprised that this relational filter is an issue with directus even though it's out there for years. I've planned everything relying on Directus because of it's awesome features (insights, flows, a great dashboard) and never thought I would face this type of issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 📋 Backlog
Development

No branches or pull requests

2 participants