Laravelでマイグレーション実行時の外部キー設定が上手くいきません。
解決済
回答 1
投稿
- 評価
- クリップ 0
- VIEW 1,462

退会済みユーザー
Laravel5.4でマイグレーションファイルを生成しようと試みているのですが、外部キー周りの設定でエラーが出てしまっているようです。
mysqlの知識が疎いのですが、外部キーの設定方法が誤っているのでしょうか?
productsテーブルとcustomersテーブルにはそれぞれ、product_idカラムとcustomer_idカラムがあります(autoincrement)
ordersテーブルからそれらのカラムに外部キー制約を貼ろうとしているのですが。。。
[vagrant@localhost door]$ php artisan migrate
Migration table created successfully.
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `orders` add constraint `orders_product_id_foreign` foreign key (`product_id`) references `products` (`product_id`))
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
CreateOrdersTable
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->increments('order_id');
$table->integer("product_id")->unsigned();
$table->foreign('product_id')->references('product_id')->on('products');
$table->integer("customer_id")->unsigned();
$table->foreign('customer_id')->references('customer_id')->on('customers');
$table->timestamps();
$table->softDeletes();
$table->engine = 'InnoDB';
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
}
CreateProductsTable
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('product_id');
$table->string('product_name')->unique("product_name");
$table->timestamps();
$table->softDeletes();
$table->engine = 'InnoDB';
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
CreateCustomersTable
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCustomersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->increments('customer_id');
$table->integer("building_id")->unsigned();
$table->foreign('building_id')->references('building_id')->on('buildings');
$table->integer("room_id")->unsigned();
$table->foreign('room_id')->references('room_id')->on('rooms');
$table->string("last_name")->nullable();
$table->string("first_name")->nullable();
$table->string("phone_number")->nullable();
$table->enum("choices", ["male, female"]);
$table->text('memo')->nullable();
$table->timestamps();
$table->softDeletes();
$table->engine = 'InnoDB';
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('customers');
}
}
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
check解決した方法
0
マイグレーションファイルの生成される順番が問題でした。
まだ、customers用のマイグレーションが実行されるまえに、customersとproductsに対して外部キー制約を設定しようとしているordersが生成されようとしていたことが原因だったみたいです。
一旦、ordersのマイグレーション生成を除外して、後から生成したら全部パスしました。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 89.99%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2017/05/05 21:24
自己解決後に発見しました。