はじめに
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 tablecustomer_point_events
add constraintcustomer_point_events_customer_id_foreign
foreign key (customer_id
) referencescustomer
(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_events
のcustomer_id
に、customer
のid
カラムとの外部キーを持たせたい
ソースコード
以下がマイグレーションファイル
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 tablecustomer_point_events
add constraintcustomer_point_events_customer_id_foreign
foreign key (customer_id
) referencescustomer
(id
))
おん?と思い、色々調べる
やったこと
いろんな記事を書いていただいていたので、たすかった・・・
と思いきや、解決せず。。。
参考記事
参照しようとしているカラムがユニークじゃない、だったり
https://qiita.com/ymzkjpx/items/a4dc01c2a7da32bb8712
「外部キーの参照先」と「参照元」のデータ型が一致していないことによるエラーだったり
https://qiita.com/Masahiro111/items/71e645923003d9a10f9e
そこで、SQLを再度確認
SQL: alter table
customer_point_events
add constraintcustomer_point_events_customer_id_foreign
foreign key (customer_id
) referencescustomer
(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"
なんてエラーが出るので、もっと違う要素が原因かと思ったら、もっとチープなモノでした。。。
今一度、カラム名、テーブル名に目を落としていただくとすぐにエラー解決するかもしれないです。