![](/img/trans.png)
[英]laravel migration SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
[英]getting 'SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint' error with migration
試圖創建 3 個表,分別是權限、users_roles 和 permission_role。 但是遷移時出現錯誤。
SQLSTATE [HY000]:一般錯誤:1215 無法添加外鍵約束(SQL:alter table permission_role
添加約束permission_role_permission_id_foreign
外鍵( permission_id
)在刪除級聯時引用permissions
( id
))
這是我的遷移:
public function up()
{
Schema::create('users_roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('user_id');
$table->nullableTimestamps();
});
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('definition')->nullable();
$table->nullableTimestamps();
});
}
public function down()
{
Schema::dropIfExists('users_roles');
Schema::dropIfExists('permissions');
}
public function up()
{
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->nullableTimestamps();
$table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('users_roles')->onDelete('cascade');
$table->primary(['permission_id', 'role_id']);
});
}
public function down()
{
Schema::dropIfExists('permission_roles');
}
我使用 unsigned() 並且不明白我的代碼有什么問題。
因為 Laravel 5.8 添加bigIncrements
和bigInteger
作為默認值。 Laravel 5.8 有一個官方升級指南中沒有提到的變化。
有兩種方法可以修復它:
將原始增量()更改為大增量並在您的外鍵列中執行大整數( )而不是整數()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.