无法添加外键约束laravel

时间:2015-05-31 16:41:51

标签: php laravel migration laravel-5

我有这个迁移:

文章

public function up()
{
    Schema::create('articles', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->integer('category_id')->unsigned();
        $table->string('title');
        $table->string('slug')->unique();
        $table->text('body');
        $table->timestamps();

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

        $table->foreign('category_id')
            ->references('id')
            ->on('categories')
            ->onDelete('cascade');
    });
}

对于类别

public function up()
{
    Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });
}

当我尝试运行命令php artisan migrate时,出现错误:

General error: 1215 Cannot add foreign key constraint (SQL: alter table`articles` add constraint articles_category_id_foreign foreign key (`category_id`) references `categories` (`id`) on delete cascade)

多次查看我发现的所有问题。

0 个答案:

没有答案