This project is archived and is in readonly mode.
Empty array interpolates as NULL in where condition
-
Mat Schaffer
Update: Used [''] as a workaround on sqlite. This doesn't seem to work on PostgreSQL.
-
Mat Schaffer
Looks like both sqlite and PostgreSQL adapters (tested via heroku's console) both generate the NULL. PostgreSQL seems unhappy with the empty array though since it considers (id NOT IN ()) to be invalid.
-
2kan
@Mat Why do you think that
id NOT IN (NULL)is incorrect?
For me everything works correct.
-
Mat Schaffer
Originally I was expecting NOT IN () which at least under sqlite had the intended effect of giving me all records. But I'm pretty sure postgres didn't like that either. Right now I have an "if ids.empty?" in my model. But that seemed less than awesome.
Is there maybe a better way to do this using an arel condition that I missed?
-
2kan
- Tag set to arel rails3, bug
@Mat, Oh, I think maybe you are right, and it is a bug, because in Postgres this query will return empty set of records, so in Ruby you will get an empty array. And I think we expect all records.
-
gmile
@2kan, going further, does
ever works for you? For me, it seems that MySQL doesn't want to retrieve any records, while trying to do something like (codes from head):IN (NULL)
The need to retrieve usingPost.all( :joins => "LEFT JOIN view_logs ON posts.id = view_logs.post_id", :conditions => { :view_logs => { :user_id => [1, 2, nil] } }nilmay appear when using, for exampleLEFT JOIN, where associated records with nulls may appear. I believe, the true retrieve of records with nulls in terms of SQL should look like
Thus, I believe, the aboveview_logs.user_id IS NULLPost.all(...)retrieval should generate the following SQL piece:
I've made an Arel patch to fix the issue, as well as a patch with a couple of Rails test to check the behavior. Could you plese take a look on them and say what you think?... WHERE (view_logs.user_id IN (1, 2) OR view_logs.user_id IS NULL)
