操作'='的非法混合排序(utf8mb4_unicode_ci,EXPLICIT)和(utf8_general_ci,COERCIBLE)

Jar*_*ard 11 mysql ruby-on-rails collation utf-8

好吧,我放弃了.我有2天这个错误,我需要帮助.

免责声明:我需要帮助改进这个问题,并会尽力描述手头的问题,到目前为止我已经做了什么来解决这个问题,并分享博客文章和我读过的文件解.

问题(也在下文中提到):

所以问题是,为什么从Rails运行而不是从mysql命令行运行时,相同的查询表现不同?具体来说,"(utf8_general_ci,COERCIBLE)"来自哪里?

问题:Autoresponder.find_by(keyword: '')失败并出现以下错误:

ActiveRecord::StatementInvalid: Mysql2::Error: Illegal mix of collations 
(utf8mb4_unicode_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) 
for operation '=': 
SELECT  `autoresponders`.* 
FROM `autoresponders`  
WHERE `autoresponders`.`keyword` = '' 
LIMIT 1
Run Code Online (Sandbox Code Playgroud)

Autoresponder 是具有该属性的模型 keyword

我读到我需要指定我的校对.所以我测试了以下代码:

Autoresponder.where('keyword collate utf8mb4_unicode_ci = ?', '')
Run Code Online (Sandbox Code Playgroud)

并得到以下错误:

Illegal mix of collations 
(utf8mb4_unicode_ci,EXPLICIT) and (utf8_general_ci,COERCIBLE) 
for operation '=': 
SELECT `autoresponders`.* 
FROM `autoresponders`  
WHERE (keyword collate utf8mb4_unicode_ci = '')
Run Code Online (Sandbox Code Playgroud)

所做的一切都是将整理从IMPLICIT改为EXPLICIT.

我尝试在Sequel Pro中运行查询并且它有效(使用和不使用collat​​e关键字).为清楚起见,这里是查询:

SELECT `autoresponders`.* 
FROM `autoresponders`  
WHERE (keyword collate utf8mb4_unicode_ci = '');

SELECT `autoresponders`.* 
FROM `autoresponders`  
WHERE (keyword = ' ');
Run Code Online (Sandbox Code Playgroud)

它有效!查询运行没有错误.我也跑了,mysql并且能够在那里运行查询.但是当我将查询粘贴到mysql命令行时,我注意到了一些问题.它自动为字符使用Unicode名称而不是实际字符.以下是在mysql命令行中观察到的查询:

SELECT `autoresponders`.* 
FROM `autoresponders`  
WHERE (keyword collate utf8mb4_unicode_ci ='\U+1F615');
Run Code Online (Sandbox Code Playgroud)

此查询有效.

所以问题是,为什么同样的查询在Rails中失败但在Sequel Pro中工作?具体来说,"(utf8_general_ci,COERCIBLE)"来自哪里,我该如何解决这个烂摊子?

我认为它可能来自ActiveRecord,但ActiveRecord::Base.connection.collation在rails console中运行返回utf8mb4_unicode_ci

这是我的db字符编码和校对变量(以及检索它们的查询).

SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
character_set_client        utf8mb4
character_set_connection    utf8mb4
character_set_database      utf8mb4
character_set_filesystem    binary
character_set_results       utf8mb4
character_set_server        latin1
character_set_system        utf8
collation_connection        utf8mb4_unicode_ci
collation_database          utf8mb4_unicode_ci
collation_server            latin1_swedish_ci
Run Code Online (Sandbox Code Playgroud)

以下是Autorsponders表的创建语法:

CREATE TABLE `autoresponders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `keyword` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT '',
  `body` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `provisioned_number_id` int(11) DEFAULT NULL,
  `outgoing_provisioned_number_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;
Run Code Online (Sandbox Code Playgroud)

上下文:Rails 4.0.13,Mysql版本5.6.22-1 + deb.sury.org~precision + 1-log

以下是我到目前为止阅读的一些博文和SO文章:https: //mathiasbynens.be/notes/mysql-utf8mb4

http://airbladesoftware.com/notes/fixing-mysql-illegal-mix-of-collat​​ions/

是否需要"SET CHARACTER SET utf8"?

操作'='的非法混合排序(utf8_unicode_ci,IMPLICIT)和(utf8_general_ci,IMPLICIT)

不具有区分大小写的搜索活动记录

https://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_collat​​ion_server

所有这一切都促使我创造了这个模因:

在此输入图像描述

此致

一个筋疲力尽的开发者.

谢谢.

toi*_*ien 12

我遇到了类似的问题并最终解决了.起初,我的MySQL配置是:

character-set-server = utf8
collation-server     = utf8_general_ci
Run Code Online (Sandbox Code Playgroud)

有一天,我发现表情符号只能用utf8mb4正确保存,所以我更改了指定列的字符集和排序规则,如下所示:

  `nickname` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切仍然很好,数据可以保存并由java Web应用程序正确显示.

但当我查询数据时

SELECT * FROM table_name WHERE nickname LIKE '%%';
Run Code Online (Sandbox Code Playgroud)

错误加剧.

最后我改变了mysql conf

character-set-server = utf8mb4
collation-server     = utf8mb4_unicode_ci
Run Code Online (Sandbox Code Playgroud)

一切都很好.

这是mysql客户端的截图,注意昵称属性


mum*_*asa 8

我遇到了同样的错误,我在 where 语句之后添加了整理

SELECT *  FROM chat_words where source ='forum';
Run Code Online (Sandbox Code Playgroud)

对操作“=”抛出非法的排序规则 (utf8mb4_unicode_ci,COERCIBLE) 和 (utf8mb4_general_ci,COERCIBLE) 混合

我后来追问

SELECT *  FROM chat_words where source collate utf8mb4_unicode_ci ='forum';
Run Code Online (Sandbox Code Playgroud)

这次运行没有错误