fjyaniez fjyaniez - 2 years ago 124
SQL Question

mysql2 gem, Rails 3.0.3 and "incompatible character encodings" errors

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

I have read everywhere about this, but I cant manage to fix it.


config.encoding = "utf-8"


adapter: mysql2
encoding: utf8
database: rails3_development
username: root
host: localhost


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)

Answer Source

I have a similar problem: a varchar field with collation utf8_bin having an ASCII-8BIT encoding.

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.

Please test if the problem goes away when you switch to ruby-mysql.

The following code, run from irb on ruby 1.9.2, demonstrates the problem:

require 'mysql2'
c = "localhost", username: "root", database: 'd')
c.query("select word from t where word = 'a'").to_a[0]["word"].encoding
# => #<Encoding:ASCII-8BIT>

This on a mysql database where every conceivable setting has been set to a utf8_bin collation.

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 ...

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download