我遇到這個問題,當我runn遷移iv得到這個錯誤「[Illuminate \ Database \ QueryException] SQLSTATE [HY000]:一般錯誤:1005無法創建表projecto_app
。#sql-118c_1d
(錯誤:150 「外鍵約束是不正確形成 」)(SQL:ALTER TABLE parks
附加約束parks_user_id_foreign
外鍵(user_id
)引用users
(id
)上刪除C ascade)」Laravel 5.3遷移外鍵約束不正確
的用戶表創建第一個是創建表個公園
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateParksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('parks', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('name',100);
$table->string('address',100);
$table->string('post-code',6);
$table->string('city',20);
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('parks');
}
}
的文件名是: 2017_01_12_000000_create_users_table 2017_02_09_111834_create_parks_table
所以你可以看到tkat有PK比公園需要FK user_is早些時候,用戶表。