简体   繁体   English

Rails应用程序的utf8mb4字符集

[英]utf8mb4 character set for Rails application

I am using MySQL version 5.7.12; 我正在使用MySQL版本5.7.12; I am trying to add emoji support in a text field. 我正在尝试在文本字段中添加表情符号支持。 I found this article. 我找到了这篇文章。 It states to alter the table and column to enable utf8mb4, which supports true unicode, including 4 byte unicode characters. 它指出要更改表和列以启用utf8mb4,它支持真正的unicode,包括4字节unicode字符。 I have a table comments and text field content, so I perform the following operation: 我有一个表注释和文本字段内容,因此我执行以下操作:

class ConvertCommentsToUtf8mb4 < ActiveRecord::Migration
  def change
    # for each table that will store unicode execute:
    execute "ALTER TABLE comments CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"
    # for each string/text column with unicode content execute:
    execute "ALTER TABLE comments CHANGE content TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"
  end
end

When I try to run a migration, I get the following error: 当我尝试运行迁移时,出现以下错误:

Mysql2::Error: You have an error in your SQL syntax; Mysql2 :: Error:您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET utf8mb4 COLLATE utf8mb4_bin' at line 1: ALTER TABLE comments CHANGE content TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin/Users/myuser/.rvm/gems/ruby-2.1.2@core/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `_query' 在第1行的'SET utf8mb4 COLLATE utf8mb4_bin'附近,请检查与您的MySQL服务器版本相对应的手册以使用正确的语法:ALTER TABLE注释更改内容TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin / Users / myuser / .rvm / gems / ruby​​- 2.1.2@core/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:在_query中

What might I be doing wrong? 我可能做错了什么?

When you use ALTER TABLE...CHANGE you need to give the column name twice, because CHANGE allows you to change the column name. 当您使用ALTER TABLE...CHANGE您需要输入两次列名,因为CHANGE允许您更改列名。

ALTER TABLE comments CHANGE content TEXT CHARACTER SET ...

In this case, it thought you were changing the column name to TEXT, with data type CHARACTER (which is a legit data type, it's a synonym for CHAR(1) ), and then it got confused when it found the word SET . 在这种情况下,它认为您正在将列名称更改为TEXT,数据类型为CHARACTER(这是合法数据类型,它是CHAR(1)的同义词),然后在找到单词SET时感到困惑。

So you can fix it in either of two ways: 因此,您可以通过以下两种方式之一对其进行修复:

ALTER TABLE comments CHANGE content content TEXT CHARACTER SET ...

ALTER TABLE comments MODIFY content TEXT CHARACTER SET ...

MODIFY is just like CHANGE but can't change the name of the column, so you don't need to spell out the column name twice. MODIFY就像CHANGE一样,但是不能更改列的名称,因此您不需要两次拼出列名。

That's the explanation for the syntax error, but FWIW you don't need to modify your text column after using CONVERT TO CHARACTER SET for the whole table. 这就是语法错误的解释,但是FWIW您不需要在对整个表使用CONVERT TO CHARACTER SET之后修改文本列。 MySQL automatically changes the character set and collation for all string columns in the table. MySQL自动更改表中所有字符串列的字符集和排序规则。


Demo: 演示:

mysql> create table comments ( content text ) character set utf8;
Query OK, 0 rows affected (0.03 sec)

mysql> alter table comments convert to character set utf8mb4;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> insert into comments set content = 0xF09F92A9;
Query OK, 1 row affected (0.01 sec)

mysql> select * from comments;
+---------+
| content |
+---------+
| 💩        |
+---------+
sql = "ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4  
COLLATE utf8mb4_unicode_ci;"
records_array = ActiveRecord::Base.connection.execute(sql)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM