1
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 3 years have passed since last update.

# [Laravel]Foreign key constraint is incorrectly formedの解決法(結論:タイポ)

Posted at

はじめに

Laravelでphp artisan migrateしようとした時に以下エラーが表示され、ちょいちょい時間溶かしたのでメモ

SQLSTATE[HY000]: General error: 1005 Can't create table app_test.customer_point_events (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table customer_point_events add constraint customer_point_events_customer_id_foreign foreign key (customer_id) references customer (id))

内容

やりたいコト

以下2つのテーブルを生成

  • customer
カラム名 制御 内容
id bigint PK id
name string 名前
created_at timestamp 作成日時
updated_at timestamp 更新日時
  • customer_point_events
カラム名 制御 内容
id bigint PK id
customer_id int FK 名前

| updated_at | timestamp | | 更新日時 |

customer_point_eventscustomer_idに、customeridカラムとの外部キーを持たせたい

ソースコード

以下がマイグレーションファイル

2022_03_05_081728_create_customers_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCustomersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('customers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('customers');
    }
}

2022_03_05_082143_create_customer_points_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCustomerPointsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('customer_points', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('customer_id');
            $table->integer('point');
            $table->foreign('customer_id')->references('id')->on('customer'); //外部キー参照
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('customer_points');
    }
}

これでヨシ、ということで以下実行

php artisan migtrate

起こったこと

冒頭のエラ―が発生

General error: 1005 Can't create table app_test.customer_point_events (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table customer_point_events add constraint customer_point_events_customer_id_foreign foreign key (customer_id) references customer (id))

おん?と思い、色々調べる

やったこと

いろんな記事を書いていただいていたので、たすかった・・・

と思いきや、解決せず。。。

参考記事

参照しようとしているカラムがユニークじゃない、だったり

https://qiita.com/ymzkjpx/items/a4dc01c2a7da32bb8712

「外部キーの参照先」と「参照元」のデータ型が一致していないことによるエラーだったり

https://qiita.com/Masahiro111/items/71e645923003d9a10f9e

そこで、SQLを再度確認

SQL: alter table customer_point_events add constraint customer_point_events_customer_id_foreign foreign key (customer_id) references customer (id)

…。

原因

タイポです。。。

references の引数のテーブル名がcustomersではなく、customerになっている…

以下修正して再度php artisan migration実行

2022_03_05_082143_create_customer_points_table.php

    public function up()
    {
        Schema::create('customer_points', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('customer_id');
            $table->integer('point');
            $table->foreign('customer_id')->references('id')->on('customers'); // 'customers'に変更
            $table->timestamps();
        });
    }

おわりに

しょうもないミスで久しぶりに時間溶かしたので、同じミスをした方用に残しておきます。。。

errno: 150 "Foreign key constraint is incorrectly formed"なんてエラーが出るので、もっと違う要素が原因かと思ったら、もっとチープなモノでした。。。

今一度、カラム名、テーブル名に目を落としていただくとすぐにエラー解決するかもしれないです。

1
0
1

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