简体   繁体   English

Laravel迁移:无法添加外键约束

[英]Laravel Migrations: Cannot add foreign key constraint

I am trying to add foreign key constraints to my database tables via Laravel Migrations, but I always get an error like this: 我试图通过Laravel Migrations将外键约束添加到我的数据库表中,但是我总是收到这样的错误:

Illuminate\Database\QueryException  :
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
(SQL: alter table `tasks` add constraint `tasks_task_list_id_foreign` foreign key (`task_list_id`) references `task_lists` (`id`))

The migration for the tasks table looks like this: tasks表的迁移如下所示:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTasksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tasks', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned()->index();
            $table->integer('task_list_id')->unsigned()->index();
            $table->string('task');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('task_list_id')->references('id')->on('task_lists');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tasks');
    }
}

And that's the task_lists table: 这就是task_lists表:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTaskListsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('task_lists', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned()->index();
            $table->string('name');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('task_lists');
    }
}

I can't figure out the problem and would highly appreciate any kind of help. 我无法解决问题,非常感谢您提供任何帮助。

Thank you in advance! 先感谢您!

You have to check the order of your migrations. 您必须检查迁移顺序。 Obviously, you have to run de Users table migration before the Tasks table migration. 显然,您必须先执行de Users表迁移,然后再执行Tasks表迁移。 If the problem persist, try to make something like this inside the up() method and after the (Schemma::create): 如果问题仍然存在,请尝试在up()方法内部以及(Schemma :: create)之后进行如下操作:

    Schema::table('task_lists', function($table){
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM