LoginSignup
0
1

More than 5 years have passed since last update.

絵文字保存時に Incorrect string value: '\xF0\x 9F\x98\x84\xF0\x9F...'

Last updated at Posted at 2016-09-05

状況

development環境では動作するがproduction環境で動かなくてはまった。
エラーでとる。

Incorrect string value: '\xF0\x9F\x99\x80' for column `emoji_text` at row 1

文字コードに違いない。

とりあえず動かす

MySQL上で絵文字を保存するテーブルの文字コードが「utf8mb4」になっていなかった。

show create table emojina_contents;
ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC

alter table emojina_contents convert to character set utf8mb4;

show create table emojina_contents;
ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC

原因

さて、では、なぜmigrationしたときに utf8 で作成されたのか。

答えは、データベースの文字コードが間違ってた。

show create table emojina_db;
/*!40100 DEFAULT CHARACTER SET utf8 */ |

DBをSQLでつくってしまったのだろう。
rake db:create ならば問題ないのに。

SQLでDBを作るときは、明示的に指定する必要ありです。(もちろんmy.cnfでも可)

create database emojina_db CHARACTER SET = 'utf8mb4';
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1