我已經啓動了一個新的laravel 5.5項目,並且在嘗試向我的users
表添加外鍵時收到以下錯誤:一般錯誤:1215無法添加外鍵約束( SQL:在刪除級聯改變表users
附加約束users_organization_id_foreign
外鍵(organization_id
)引用organizations
(id
))使用Laravel遷移添加外鍵約束
下面是organization
表遷移代碼:
Schema::create('organizations', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('subdomain')->nullable();
$table->timestamps();
$table->softDeletes();
});
下面是users
表遷移代碼:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('organization_id')->unsigned();
$table->foreign('organization_id')->references('id')->on('organizations');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->timestamps();
$table->softDeletes();
});
我在網上研究了這個錯誤,並確保的是數據類型相同的普通的事。從我所看到的,他們是一樣的。什麼是更瘋狂的是,如果我在數據庫運行此查詢直接它的工作原理:
alter table `users` add constraint `users_organization_id_foreign` foreign key (`organization_id`) references `organizations` (`id`)
任何幫助,不勝感激!
您是否將dbal添加到您的composer.json?如果不添加,作曲家需要doctrine/dbal – pseudoanime