mysql2 gem,Rails 3.0.3和“不兼容字符编码”错误 [英] mysql2 gem, Rails 3.0.3 and "incompatible character encodings" errors
问题描述
我使用Rails 3.0.3,我已经将mysql适配器从ruby-mysql更改为mysql2,但现在我有以下错误:
I'm using Rails 3.0.3, and I have changed the mysql adapter from ruby-mysql to mysql2, but now I have the following error:
incompatible character encodings: ASCII-8BIT and UTF-8
application.rb: b
config.encoding = "utf-8"
database.yml:
database.yml:
development:
adapter: mysql2
encoding: utf8
database: rails3_development
username: root
password:
host: localhost
宝石:
specs:
abstract (1.0.0)
actionmailer (3.0.3)
actionpack (3.0.3)
activemodel (3.0.3)
activerecord (3.0.3)
activeresource (3.0.3)
activesupport (3.0.3)
arel (2.0.7)
bcrypt-ruby (2.1.4)
builder (2.1.2)
erubis (2.6.6)
i18n (0.5.0)
jquery-rails (0.2.6)
mail (2.2.15)
mime-types (1.16)
**mysql2 (0.2.6)
orm_adapter (0.0.4)
paperclip (2.3.8)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack-test (0.5.7)
rails (3.0.3)
railties (3.0.3)
rake (0.8.7)
thor (0.14.6)
treetop (1.4.9)
tzinfo (0.3.24)
warden (1.0.3)
will_paginate (3.0.pre2)
推荐答案
我有一个类似的问题:varchar字段,具有ASCII-8BIT编码的排序规则utf8_bin。
I have a similar problem: a varchar field with collation utf8_bin having an ASCII-8BIT encoding.
问题出在mysql2 gem,不是在Rails,在mysql设置,至少在我的情况下,因为它不会发生ruby-mysql宝石。
The problem lies in the mysql2 gem, not in Rails, nor in the mysql settings, in my case at least, because it doesn't occur with the ruby-mysql gem.
请测试当您切换到ruby-mysql时问题是否消失。
Please test if the problem goes away when you switch to ruby-mysql.
从irb on ruby 1.9.2,演示了问题:
The following code, run from irb on ruby 1.9.2, demonstrates the problem:
require 'mysql2'
c = Mysql2::Client.new(host: "localhost", username: "root", database: 'd')
c.query("select word from t where word = 'a'").to_a[0]["word"].encoding
# => #<Encoding:ASCII-8BIT>
在一个mysql数据库上,每个可能的设置都设置为utf8_bin排序规则。
This on a mysql database where every conceivable setting has been set to a utf8_bin collation.
在mysql2 gem中,在第253行的 result.c
文件中有以下代码片段:
In the mysql2 gem, in the result.c
file on line 253, there's the following snippet:
if (fields[i].flags & BINARY_FLAG) {
rb_enc_associate(val, binaryEncoding);
} else ...
我相信这是二进制编码被设置,也许是因为utf8_bin排序规则...我已经删除它,它解决了问题,但我相信它可能会引入其他问题,与blobs为例。
I believe this is where the binary (ASCII-8BIT) encoding is being set, maybe because of the utf8_bin collation... I've removed it, and it solved the problem, but I'm sure it will probably introduce other problems, with blobs for instance.
这篇关于mysql2 gem,Rails 3.0.3和“不兼容字符编码”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!