SQLSTATE [HY000]:常规错误:1215无法添加外键约束Laravel [英] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint Laravel

查看:235
本文介绍了SQLSTATE [HY000]:常规错误:1215无法添加外键约束Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用artisan创建外键,但是会显示此错误.

Im trying to create a foreign keys using artisan, but this error show up.

[Illuminate\Database\QueryException]                                                                                                                                                                             
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `comments` add constraint `comments_comment_lot_id_foreign` foreign key (`comment_lot_id`) references `lots` (`lot_id`  
  ) on delete cascade) 

这是我的迁移:

<?php

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

class CreateCommentsTable extends Migration
{

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->text('comment');
            $table->integer('comment_lot_id')->unsigned();
            $table->timestamps();
        });

        Schema::table('comments', function ($table) {
            $table->foreign('comment_lot_id')->references('lot_id')->on('lots')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropForeign(['comment_lot_id']);
        Schema::dropIfExists('comments');
    }
}

在lots表中,我使用lot_id作为id,它对Lot.php进行建模:

in the lots table i use lot_id as id it model Lot.php i add:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Lot extends Model {
    protected $primaryKey = 'lot_id';

}

任何想法我该如何解决此错误?

Any idea how can i resolve this error?

推荐答案

将以下这些规则应用于您的迁移文件:

[1]

父数据透视表必须基于支持以下内容的引擎 外键引用(例如,用于mysql的InnoDB).

The parent, pivot table(s) must be based on engines that supports foreign key referencing (e.g InnoDB for mysql).

执行$table->engine = InnoDB; 在迁移文件中,就在其他列定义之前.

Do $table->engine = "InnoDB"; in your migration file, right before other column definitions.

我观察到laravel始终默认为MyISAM,因此这行是必须的.

I observe laravel always default to MyISAM hence this line is a must.

[2]

父级中引用的列必须是主列或唯一的 键.

The referenced columns in the parent must be a primary or unique key(s).

父表中的这些声明很好:

These declarations in the parent table are fine:

$table->increments(id);表示列"id"是可引用的

$table->increments("id"); means column "id" is referencable

$table->column_type(column_name)->unique();表示列"column_name"是可引用的

$table->column_type("column_name")->unique(); means column "column_name" is referencable

[3]

数据透视表列必须与其列类型相同 引用的父表列.

The pivot table column must be of the same type as that of its referenced parent table column.

例如,应引用增量("id")的数据透视表列必须为unsignedInteger类型.

So for example, the pivot table column that should reference increments("id") must of type of unsignedInteger.

如果父表的类型为char(20),则用于引用它的数据透视表列也必须为char(20).

If parent table is type char(20), then pivot table column used to reference it must be type char(20) as well.

已完成上述所有三个操作,请适当地定义您的外键关系.

这篇关于SQLSTATE [HY000]:常规错误:1215无法添加外键约束Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆