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'); } }

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/05/05 12:24