3

I have a new table I am adding to a large Ruby on Rails project. Our database currently uses the UTF8 character encoding. However, I need to support emoji's in one column of this new table.

In order to do that, that column needs to be in UTF8mb4 encoding, since the base UTF8 encoding in MySQL doesn't support emoji's.

I can change things on the mysql side by executing this SQL:

execute "ALTER TABLE table_name CHANGE column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin"

The problem is this doesn't fix the issue, I actually have to change the 'encoding' option in the database.yml file from utf8 to utf8mb4.

The problem with this is when my test run loads the database schema, suddenly it can't because characters in utf8mb4 use more bytes than they do in utf8, and we have existing indexes that are too long to fit into the new utf8mb4 byte requirement.

I don't really want to edit all of our existing indexes that break this, our database is pretty large.

Is there no way in Rails to use a character encoding on a table or column level? Do I have to update the database.yml file and therefore force my entire database to use utf8mb4?

1
  • Workaround, you can serialize value before save it to utf-8 database. Commented Nov 19, 2016 at 16:34

1 Answer 1

2
  • Change VARCHAR(255) to VARCHAR(191), for any column being indexed, to get around index limit, or
  • Update to MySQL 5.7, which does not have the issue.

If you need to discuss more, please provide SHOW CREATE TABLE and the specific connection parameters, perhaps found in application_controller.rb.

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.