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

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

问题描述

我正在尝试使用 artisan 创建外键,但出现此错误.

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

[IlluminateDatabaseQueryException]                                                                                                                                                                             
  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 IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

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');
    }
}

在手数表中,我使用 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 IlluminateDatabaseEloquentModel;

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

}

知道如何解决此错误吗?

Any idea how can i resolve this error?

推荐答案

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

[1]

父数据透视表必须基于支持外键引用(例如 InnoDB for mysql).

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.

例如,应该引用 increments(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天全站免登陆