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

编程入门 行业动态 更新时间:2024-10-26 00:26:47
本文介绍了SQLSTATE [HY000]:常规错误:1215无法添加外键约束-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试创建一个表格来保存照片并将其链接到广告(属性ID). 我收到此错误

I'm trying to create a table to hold photos and link it to the ad (property id). I'm getting this error

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

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

这些是我的迁移文件

2018_02_14_191609_create_property_adverts_table

2018_02_14_191609_create_property_adverts_table

<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePropertyAdvertsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('property_adverts', function (Blueprint $table) { $table->increments('id'); $table->string('address'); $table->string('county'); $table->string('town'); $table->string('type'); $table->string('rent'); $table->string('date'); $table->string('bedrooms'); $table->string('bathrooms'); $table->string('furnished'); $table->longText('description'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('property_adverts'); } }

2018_02_18_165845_create_property_advert_photos_table

2018_02_18_165845_create_property_advert_photos_table

<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePropertyAdvertPhotosTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('property_advert_photos', function (Blueprint $table) { $table->increments('id'); $table->integer('propertyadvert_id')->nullable(); $table->foreign('propertyadvert_id')->references('id')->on('property_adverts'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('property_advert_photos'); } }

所以

推荐答案

将其设为unsigned,因为您正在使用increments().并将FK约束部分移至单独的闭包中:

Make it unsigned because you're using increments(). And move FK constraint part to a separate closure:

public function up() { Schema::create('property_advert_photos', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('propertyadvert_id')->nullable(); $table->timestamps(); }); Schema::table('property_advert_photos', function (Blueprint $table) { $table->foreign('propertyadvert_id')->references('id')->on('property_adverts'); }); }

更多推荐

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

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

发布评论

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

>www.elefans.com

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