【问题标题】:SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint LaravelSQLSTATE[HY000]:一般错误:1215 无法添加外键约束 Laravel
【发布时间】:2017-04-28 06:10:05
【问题描述】:

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

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

在批次表中,我使用 lot_id 作为 id 它模型 Lot.php 我添加:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

}

知道如何解决此错误吗?

【问题讨论】:

  • 如下所述,确保两个整数完全相同,无符号,长度等。这通常是失败的原因。
  • 要查找特定错误,请在数据库电机上运行 SHOW ENGINE INNODB STATUS;。学分stackoverflow.com/questions/15534977/…

标签: laravel-5 foreign-keys migration


【解决方案1】:

看起来这对你来说不是问题,但我在 Laravel 5.8 中遇到了同样的错误,并发现了一个有趣的问题:Laravel 现在默认将 'id' 列设置为 'bigIncrements' 而不仅仅是 'increments'。因此,不要像以前那样用 'integer' 来引用它,而必须用 'bigInteger' 来引用它。

如果您的父表如下所示:

$table->bigIncrements('id');

那么子迁移需要如下所示:

$table->bigInteger('parent_id')->unsigned()->index();
$table->foreign('parent_id')->references('id')->on('parent');

希望这可以帮助其他在 5.8 及更高版本中遇到此问题的人。

【讨论】:

  • 所以这有很大帮助。我在 laravel 5.8 中遇到了同样的问题。大起大落
  • 在现有应用程序中创建每个新模型时,您可以将其从 bigIncrements('id') 切换到 increments( 'id') 在运行迁移之前,这将为您提供 INT 以匹配您现有的模型。考虑MySQL docs 说一个无符号的 INT 仍然会给你一个最大值 4,294,967,295。我认为这对我所做的一切来说都足够大了……
【解决方案2】:

引用this answer:

要查找特定错误,请运行以下命令:

SHOW ENGINE INNODB STATUS;

然后查看LATEST FOREIGN KEY ERROR 部分。

这可能是类型问题。 comment_lot_id 的类型必须与 lot_id 完全相同。也许一个已签名而另一个未签名。

【讨论】:

    【解决方案3】:

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

    [1]

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

    $table-&gt;engine = “InnoDB”; 在您的迁移文件中,就在其他列定义之前。

    我观察到 laravel 总是默认使用 MyISAM,因此这条线是必须的。

    [2]

    父项中的引用列必须是主列或唯一列 键。

    父表中的这些声明很好:

    $table-&gt;increments(“id”); 表示“id”列是可引用的

    $table-&gt;column_type(“column_name”)-&gt;unique(); 表示“column_name”列是可引用的

    [3]

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

    例如,应该引用增量(“id”)的数据透视表列必须是 unsignedInteger 类型。

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

    完成上述所有三个操作后,根据需要定义您的外键关系。

    【讨论】:

    • 谢谢!它起作用了。 1。 ' $table->engine = 'MyISAM'; ' 因为父表也是 MyISAM 2. $table->integer('comment_lot_id')->unique()->unsigned();到很多父表
    猜你喜欢
    • 2018-07-29
    • 2019-08-02
    • 2021-07-03
    • 2020-01-14
    • 2022-11-18
    • 2016-12-20
    • 2019-11-11
    • 2019-07-27
    • 2021-03-31
    相关资源
    最近更新 更多