SQLSTATE [HY000]:常规错误:1215无法添加外键约束Laravel

编程入门 行业动态 更新时间:2024-10-26 02:24:28
本文介绍了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

本文发布于:2023-10-26 01:17:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常规   错误   SQLSTATE   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!