LoginSignup
3
0

More than 3 years have passed since last update.

Laravel で migrate 実行時に migrations テーブルの id が重複した

Last updated at Posted at 2019-04-02

概要

Laravel デプロイ時、何故か migrate コマンドで失敗した正常にデプロイできなかったので、その時の原因の調査と解消方法をまとめます。(多分デプロイ時であるかどうかは関係ないです)

環境

  • Laravel 5.7
  • PostgreSQL
  • Deployer (デプロイツール)

起きたこと

Deployer が php artisan migrate --force を実行した時に、 PostgreSQL の migrations テーブルに既存の id と同じ値で INSERT しようとしたため、ユニーク制約が掛かっているのでダメだと言われコケたようです。


  [Deployer\Exception\RuntimeException (1)]
  The command "/usr/bin/php /var/www/html/releases/214/artisan migrate --force" failed.

  Exit Code: 1 (General error)

  Host Name: 172.30.1.94

  ================
  Migrating: 2019_04_02_path_to_migration_file

  In Connection.php line 664:

    SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value violates u
    nique constraint "migrations_pkey"
    DETAIL:  Key (id)=(1314) already exists. (SQL: insert into "migrations" ("m
    igration", "batch") values (2019_04_02_path_to_migration_file, 10))


  In PDOStatement.php line 119:

    SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value violates u
    nique constraint "migrations_pkey"
    DETAIL:  Key (id)=(1314) already exists.


  In PDOStatement.php line 117:

    SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value violates u
    nique constraint "migrations_pkey"
    DETAIL:  Key (id)=(1314) already exists.

解消手順

migrations テーブルの id カラムの sequence の last_value を以下のSQLで確認しました。

select * from migrations_id_seq

実際に last_value が、失敗した INSERT 時に指定していた値になっていたので、
これを実際の現在の最大値に設定し直しました。

select setval('migrations_id_seq', {最大値})

この状態でデプロイをやり直したら解決しました。

起きた原因

特定できませんでした :joy:

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