1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQLでUTF8からUTF8mb4エンコード変更時に出るエラー対策

1
Posted at

小メモ。
Symfony2.7系、Doctrine、MySQL5.6、な環境。

エラー内容

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes  

発生条件

  • DBとかテーブルとかカラムがutf8なやつを、utf8mb4に変更するときに発生

打ったSQL

ALTER TABLE fooooooo default character set utf8mb4;
ALTER TABLE fooooooo CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

原因

Entityとか

  • Doctrineを使ってる。
  • UTF8は3バイト、ソースコード上はMAX255文字。
  • 255文字*3バイトで765Byte。ユニーク制約付きvarcharは767byteまでなので、セフセフ。

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255, unique=true, options={"comment" = "名前"})
     * @Assert\NotBlank(message="おまえの名を入力して下さい。")
     */
    protected $name;

修正

  • そもそも名前にlengthが255とかいらんだろということで、length="30"に変更。
    • 問題無し。

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=30, unique=true, options={"comment" = "名前"})
     * @Assert\NotBlank(message="おまえの名を入力して下さい。")
     */
    protected $name;

やったね。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?