Open oddanderson opened 2 months ago
This isn't a bug. The issue you're running into here is that you're querying a column that contains NULL values. In databases, a NULL value is an unknown value, not necessarily an "empty" value. In your example, the Toronto record has a random_id of NULL. In other words, you're saying that Toronto has some random_id that you don't know the value of.
When you use .neq("random_id", random.id)
you're saying "give me all records that you know for sure have a random_id NOT EQUAL TO random.id
".
You expect to get Toronto, because the "random_id"
is defined as NULL. BUT since a NULL value is treated as unknown... you can't say for sure that it's not equal to random.id
... since it's unknown it technically COULD be equal to random.id
. Therefore it doesn't return it as a match.
"In databases" should be made more specific to "In Postgres." There are many databases that allow direct comparison operators to null values with the above expected results.
Postgres provides Is distinct from
to explicitly handle this case. Is there a reason why the NEQ implementation doesn't use it instead of a != or <>? Are there plans to provide an additional wrapper that uses is distinct from
?
It's not ideal to require users to write an RPC or create a custom view in place of using a standard operator, both of which are obfuscated when reading someone else's code. I struggle to imagine a frequent use case of someone using NEQ to a non-null value and expecting to not receive rows with null values, no matter how postgres may have chosen to implement null under-the-hood.
the NEQ is actually a PostgREST behavior (which is what Supabase uses) https://docs.postgrest.org/en/v12/references/api/tables_views.html
it looks like PostgREST already supports an "isDistinct"... I suspect it should be easy enough for the supabase team to add that in 🤷. They have a separate postgrest-js repo where they have this code... I'm assuming they just need to add it here: https://github.com/supabase/postgrest-js/blob/master/src/PostgrestFilterBuilder.ts
Bug report
Describe the bug
Unable to use database filters on inner joins for nullable foreign keys
e.g.
from("tableA").select(", tableB()").eq("tableB.column", value) --> works from("tableA").select(", tableB()").neq("tableB.column", value) --> does not work, always empty
To Reproduce
Schema:
Test Code
Output
Expected behavior
use neq random, expect canada cities --> should return canadian cities
Screenshots
If applicable, add screenshots to help explain your problem.
System information
Additional context
Add any other context about the problem here.