本文介绍了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
这些是我的迁移文件
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无法添加外键约束-Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!