LoginSignup
6
2

More than 3 years have passed since last update.

typeORM + mySQL で絵文字 🚀😀🇹🇭 が入らない問題 🤔

Posted at

mySQLデータベースはutf8mb4で作っているのにTypeORMを使うと絵文字が入らない。という問題。

typeormcreateConnectionをするとcharsetに関係なくcharset=UTF8_GENERAL_CIになってしまうため、どこかで設定する必要があります。

解決策1

ormconfig.jsoncharsetを指定する。

...
"username": "root",
"extra": {
 "charset": "utf8mb4"
},
...

解決策2

createConnectionでオプションを指定する。

index.ts
...

import { createConnection, getConnectionOptions } from 'typeorm';

...

const connectionOptions = await getConnectionOptions();
Object.assign(connectionOptions, { charset: 'utf8mb4' });

const connection = await createConnection(connectionOptions);

...

メモ

答え: https://github.com/typeorm/typeorm/issues/390

ormconfig.jsonで設定するほうが好みだったので解決策1を選択しました。上記のgithub issueには、

Just wanted to point out that setting charset inside the extra options is obsolete since commit 6aeda88 (July 2017).
Just add the charset option directly to ConnectionOptions.

とありましたが今でもextraできます。

6
2
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
6
2