ActiveRecord - 非法混合排序规则

时间:2015-06-05 23:47:24

标签: mysql ruby-on-rails ruby activerecord character-encoding

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

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),错误说我有一些拉丁语...

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

我的尝试:

  • 我尝试将所有数据库表设置为utf8排序规则。


  • 我也试过抬头:

    select collation_name from information_schema.columns where table_schema = 'redmine' and collation_name like '%' group by table_name;

    其中归类名称类似于utf%或latin%,它只显示utf8和NULL。拉丁%搜索是空的。


  • 显示变量:

    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 collation_connection utf8_general_ci collation_database latin1_swedish_ci collation_server latin1_swedish_ci

    一般来说,我猜测它看起来像数据库存储在拉丁语中,但连接是在utf8中完成的? 这会导致这个问题吗?

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

我仍然试图将其缩小到数据库或应用程序方面。

1 个答案:

答案 0 :(得分:2)

摘要

我强制进行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说最近没有对数据库进行任何更改......所以很奇怪这种错误会在它出现的时候弹出......我真的很想知道这个问题的真正原因所以我可以修改它。
我仍然怀疑与数据库排序和&amp;字符集,但有一件事让我怀疑这是因为我的开发环境机器将所有字符集和排序规则设置为UTF8 ,而我的 Prod环境数据库 < / p>

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

我仍然在Dev vs Production环境中以相同的方式得到同样的错误。