0
0

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 1 year has passed since last update.

ダンプファイルを実行して「Error Code: 1068. Multiple primary key defined」と出た時

Posted at

phpMyAdminでは「エクスポート」タブからダンプファイルを作成することができます。

詳細オプション.png

本番環境で作成したダンプファイルを開発環境のDBで実行したら、以下のようなエラーがたくさん出てしまいました。

Error Code: 1068. Multiple primary key defined

該当部分のクエリをコメントアウトしても、また次のエラーが出てくる…(エンドレス)

Error Code: 1061. Duplicate key name 'hoge'

エラーの内容から、ファイル内の「主キーを設定する」部分など、テーブルを定義するコマンド部分が重複していることが分かりました。

原因

DBとダンプファイルの「構造」が重複していたこと。

DBではマイグレーションを行っていたため、テーブルの「構造」はすでに設定されていました。
一方、ダンプする時には内容として「構造とデータ」を選択していました。

解決策

ダンプファイル作成時、「データ」のみをダンプする。

具体的には、
phpMyAdminの「エクスポート」
→詳細-可能なオプションを全て表示
→「フォーマット特有のオプション」で「データ」を選択
(エラーが出た時は、ここで「構造とデータ」を選択していた)
フォーマット.png

普段ダンプファイルはSSH接続しコマンドで作成しているので、ファイルの中身まで考えたことありませんでした…
勉強になりました。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?