范围提升MysQL错误:非法混合排序规则

时间:2012-12-05 18:01:12

标签: ruby-on-rails

非常奇怪: 我的范围正常运行:

scope :starting_at, lambda {|start_date|
    where("CAST(starts_at AS TIME) = ?", Event.db_time(start_date))
}

(rdb:1) sd1 => 2012-12-16 10:00:00 +0100
Event.starting_at(sd1)
[#<Event id: 1, account_id: 1, place_id: 1, slug: "my-new-....

我有另一个类似的范围,它会引发错误:

scope :starting_at_between, lambda {|start_time, end_time|
    where( "? <= CAST(starts_at AS TIME) <= ?", Event.db_time(start_time), Event.db_time(end_time) )
}
(rdb:1) sd2  =>  2012-12-15 08:00:00 +0100
(rdb:1) ed2  =>  2012-12-19 23:00:00 +0100
Event.starting_at_between(ed2, sd2)
INTERNAL ERROR!!! Mysql2::Error: Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,NUMERIC) for operation '<=': SELECT `events`.* FROM `events`  WHERE ('23:00:00' <= CAST(starts_at AS TIME) <= '08:00:00')

使用这个有什么问题......? 不是因为2个参数... 我有一个类似的范围,其中CAST是AS DATE并且运行良好...

scope :starting_on_between, lambda { |start_date, end_date|
    where( "? <= CAST(starts_at AS DATE) <= ?", Event.db_date(start_date), Event.db_date(end_date) )
}
Event.starting_on_between(ed2, sd2)
[#<Event id: 1, account_id: 1, place_id: 1, slug:...

1 个答案:

答案 0 :(得分:0)

错误的CAST ..应该在过滤器值上完成...而不是在db记录属性上。

范围:starting_at_between,lambda {| start_time,end_time |     where(“CAST(?AS TIME)&lt; = starts_at&lt; = CAST(?AS TIME)”,Event.db_time(start_time),Event.db_time(end_time))   }