ActiveRecord - 非法混合排序规则

ActiveRecord - illegal mix of collations

我在使用 Ruby 的 ActiveRecord 和 Redmine 应用程序时遇到一些问题。

Started PATCH "//issues/33135" for [ipaddress] at 2015-06-02 17:02:48 -0700
Processing by IssuesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"[secret_token]", "issue"=>{"is_private"=>"0", "project_id"=>"949", "tracker_id"=>"4", "subject"=>"adgsasdg", "description"=>"asdggsad", "status_id"=>"1", "priority_id"=>"1", "assigned_to_id"=>"", "parent_issue_id"=>"", "start_date"=>"2015-06-02", "due_date"=>"2015-06-17", "done_ratio"=>"0", "custom_field_values"=>{"349"=>"", "32"=>"", "33"=>"", "67"=>"", "63"=>"", "221"=>"", "209"=>"0", "362"=>"", "234"=>"", "237"=>"", "235"=>[""], "435"=>""}, "notes"=>"", "private_notes"=>"0", "lock_version"=>"0"}, "was_default_status"=>"1", "time_entry"=>{"hours"=>"", "activity_id"=>"", "comments"=>"", "custom_field_values"=>{"388"=>"", "387"=>""}}, "last_journal_id"=>"", "commit"=>"Submit", "id"=>"33135"}
  Current user: [username] (id=3)
Mysql2::Error: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (latin1_swedish_ci,NUMERIC) for operation '>': INSERT INTO `journal_details` (`property`, `prop_key`, `old_value`, `value`, `journal_id`) VALUES ('cf', '32', '2015-06-25', '', 56006)
Completed 500 Internal Server Error in 811ms (ActiveRecord: 93.6ms)

ActiveRecord::StatementInvalid (Mysql2::Error: Illegal mix of collations (utf8_general_ci,IMPLICIT) and (latin1_swedish_ci,NUMERIC) for operation '>': INSERT INTO `journal_details` (`property`, `prop_key`, `old_value`, `value`, `journal_id`) VALUES ('cf', '32', '2015-06-25', '', 56006)):
  app/models/journal.rb:67:in `save'
  app/models/issue.rb:1566:in `create_journal'
  app/models/issue.rb:175:in `create_or_update'
  app/controllers/issues_controller.rb:479:in `block in save_issue_with_child_records'
  app/controllers/issues_controller.rb:467:in `save_issue_with_child_records'
  app/controllers/issues_controller.rb:180:in `update'

我所有的表都显示 utf8 的排序规则(有些 ...general_ci 和一些 ...unicode_ci,但仍然是 UTF8),错误说我有一些 latin1...

我完全不明白这是什么意思...
这是否意味着它正试图从 utf8 转到 latin1?或相反亦然? (有关系吗?)


我尝试过的:


这是什么问题?我该如何解决?还有其他人看到类似的东西吗?

我仍在尝试将范围缩小到数据库或应用程序方面。

总结

我强制使用 UTF8 排序规则并将要比较的数据转换为 UTF8 以进行实际比较操作。


出于某种原因,我使用的数据库触发器(并且已经使用了 4 个月没有问题)是问题的根源。 我怀疑数据库发生了变化,但不确定。

在触发器中,有一行(而且只有一行)使用“>”比较运算符将 new.valuedue_date 进行比较。

我首先尝试在语句中添加 COLLATE,但后来我的错误变为:

Mysql2::Error: COLLATION 'utf8_unicode_ci' is not valid for CHARACTER SET 'latin1': INSERT INTO `journal_details` (`property`, `prop_key`, `old_value`, `value`, `journal_id`) VALUES ('cf', '32', '2015-06-16', '', 56863)
Completed 500 Internal Server Error in 846ms (ActiveRecord: 69.0ms)

ActiveRecord::StatementInvalid (Mysql2::Error: COLLATION 'utf8_unicode_ci' is not valid for CHARACTER SET 'latin1': INSERT INTO `journal_details` (`property`, `prop_key`, `old_value`, `value`, `journal_id`) VALUES ('cf', '32', '2015-06-16', '', 56863)):"

然后我尝试了:

将 Convert() 和 Collat​​e 添加到语句

NEW.value > CONVERT(Due_date USING utf8)

COLLATE utf8_unicode_ci

一切又恢复正常了。 =)



额外:

MySQL DBA 说最近没有对数据库进行任何更改...所以很奇怪这种错误会在它出现时弹出...我真的很想知道问题的真正原因,所以我可以解决这个问题。
我仍然怀疑与数据库排序规则和字符集有关,但让我怀疑的是我的 Dev 环境机器将所有字符集和排序规则设置为 UTF8,而我的 Prod 环境数据库有

character_set_client    utf8
character_set_connection    utf8
character_set_database  latin1
character_set_filesystem    binary
character_set_results   utf8
character_set_server    latin1
character_set_system    utf8
character_sets_dir  /usr/share/mysql/charsets/
collation_connection    utf8_general_ci
collation_database  latin1_swedish_ci
collation_server    latin1_swedish_ci

而且我在开发与生产环境中仍然以相同的方式遇到相同的错误。