Skip to content

Instantly share code, notes, and snippets.

@Bijendra
Created March 5, 2021 06:55
Show Gist options
  • Save Bijendra/79853cb51b63772af4f21d0a6494e45e to your computer and use it in GitHub Desktop.
Save Bijendra/79853cb51b63772af4f21d0a6494e45e to your computer and use it in GitHub Desktop.
Illegal mix of collations issue in database update operations in Rails
ERROR 1267 (HY000): Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation '<>'
Database: MySQl
Rails application with ruby 2.5.7 and Rails 5.1.
Use case: While importing database from multiple .sql files into test db via rake tasks the collation_database parameter is giving issues.
The .sql files contains seed data which is inserted successfully.
While running rspec cases to update a given table, the error related to mix of collations is thrown in rspec logs.
To resolve this issue, changes were done in database.yml file. below line was added to force the db import with specific encoding.
collation: utf8_unicode_ci
To idenfity the root cause, try with below queries as the o/p would help anyone looking into it.
state of DB in earlier imports:
mysql>show VARIABLES LIKE 'coll%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
mysql> show VARIABLES LIKE 'char%';
+--------------------------+--------------------------------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql-5.6.26-osx10.8-x86_64/share/charsets/ |
+--------------------------+--------------------------------------------------------+
After adding the change in database.yml and performing DB import.
mysql> show VARIABLES LIKE 'coll%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_unicode_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment