You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've run into a query not returning the expected result and finally got it down to this minimum working example:
classTestquerydefself.get_total(id)calendar_days=CalendarDay.using(:shard)\
.where(id: id)\
.where.not(price: nil)puts"Got #{calendar_days.count} entries"total=0.0calendar_days.map{ |cd|
total += cd.price}putstotalenddefself.get_total2(id)calendar_days=CalendarDay.using(:shard)\
.where(id: id)\
.where('price IS NOT NULL')puts"Got #{calendar_days.count} entries"total=0.0calendar_days.map{ |cd|
total += cd.price}putstotalendend
> ActiveRecord::Base.logger = Logger.new(STDOUT)
> Testquery.get_total(id)
Fetching calendar_days
D, [2019-02-22T18:41:19.231971 #20445] DEBUG -- : [Shard: shard] (1.2ms) SELECT COUNT(*) FROM "calendar_days" WHERE "calendar_days"."id" = $1 AND "calendar_days"."price" IS NOT NULL [["id", 564815]]
Got 1929 entries: CalendarDay::ActiveRecord_Relation
D, [2019-02-22T18:41:19.232863 #20445] DEBUG -- : [Shard: master] CalendarDay Load (0.5ms) SELECT "calendar_days".* FROM "calendar_days" WHERE "calendar_days"."id" = $1 AND "calendar_days"."price" IS NOT NULL [["id", 564815]]
0.0
=> nil
> Testquery.get_total2(id)
Fetching calendar_days
D, [2019-02-22T18:41:23.594372 #20445] DEBUG -- : [Shard: shard] (1.2ms) SELECT COUNT(*) FROM "calendar_days" WHERE "calendar_days"."id" = $1 AND (price IS NOT NULL) [["id", 564815]]
Got 1929 entries: CalendarDay::ActiveRecord_Relation
D, [2019-02-22T18:41:23.600031 #20445] DEBUG -- : [Shard: shard] CalendarDay Load (5.2ms) SELECT "calendar_days".* FROM "calendar_days" WHERE "calendar_days"."id" = $1 AND (price IS NOT NULL) [["id", 564815]]
312784.0
=> nil
The difference between the two queries is that the first uses the ActiveRecord way of where.not(price: nil) while the second uses where('price IS NOT NULL'). If I use where.not, the evaluation when invoking map goes to :master even though a shard is specified. Contrary to that the prior call to count correctly goes to the shard. I also tested each in place of map and both had the same result.
So the combination of defining a non-master shard, querying with where.not and then running map/each does not evaluate the query to the correct shard.
The text was updated successfully, but these errors were encountered:
I've run into a query not returning the expected result and finally got it down to this minimum working example:
The difference between the two queries is that the first uses the ActiveRecord way of
where.not(price: nil)
while the second useswhere('price IS NOT NULL')
. If I usewhere.not
, the evaluation when invokingmap
goes to:master
even though a shard is specified. Contrary to that the prior call to count correctly goes to the shard. I also testedeach
in place ofmap
and both had the same result.So the combination of defining a non-master shard, querying with
where.not
and then runningmap/each
does not evaluate the query to the correct shard.The text was updated successfully, but these errors were encountered: