简体   繁体   English

1215无法在laravel 5中添加外键约束,外键

[英]1215 Cannot add foreign key constraint in laravel 5, foreign key

2019_04_23_164151_create_contacts_table.php migration file 2019_04_23_164151_create_contacts_table.php迁移文件

<?php

 use Illuminate\Support\Facades\Schema;
 use Illuminate\Database\Schema\Blueprint;
 use Illuminate\Database\Migrations\Migration;

 class CreateContactsTable extends Migration
 {
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{

    Schema::create('contacts', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('surname');
        $table->timestamps();
    });

    Schema::create('contacts_relations', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedInteger('phone_id')->nullable()->default(1);
        $table->unsignedInteger('contact_id')->nullable()->default(1);
    });

    Schema::create('contact_phone', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->integer('phone_number');
        $table->unsignedInteger('contacts_relations_id')->nullable()->default(1);
    });

}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('contacts');
}}

2019_04_24_183110_contacts_relations.php foreign key setter 2019_04_24_183110_contacts_relations.php外键设置器

 <?php

 use Illuminate\Support\Facades\Schema;
 use Illuminate\Database\Schema\Blueprint;
 use Illuminate\Database\Migrations\Migration;

 class ContactsRelations extends Migration
 {
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::table('contacts_relations', function (Blueprint $table) {
        $table->foreign('contact_id')->references('id')->on('contacts');
    });
    Schema::table('contact_phone', function (Blueprint $table) {
        $table->foreign('contacts_relations_id')->references('id')->on('contacts_relations');
    });

}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    //
}
}

run php artisan migrate: fresh and get SQLSTATE [HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table contacts_relations add constraint contacts_relations_contact_id_foreign foreign key ( contact_id ) references contacts ( id ) on delete cascade on update cascade) 运行php artisan migrate:fresh并获取SQLSTATE [HY000]:常规错误:1215无法添加外键约束(SQL:alter table contacts_relations添加约束contacts_relations_contact_id_foreign外键( contact_id )引用contactsid )删除级联更新级联)

You should make sure all columns are eact same type. 您应该确保所有列都是相同的类型。 For example in table contacts the id column is unsigned big integer, so when you create contact_id column in contacts_relations instead of: 例如,在表contactsid列是无符号大整数,因此当您在contacts_relations创建contact_id列而不是:

$table->unsignedInteger('phone_id')->nullable()->default(1);

it should be: 它应该是:

$table->unsignedBigInteger('phone_id')->nullable()->default(1);

Similar thing for contacts_relations_id column 对于contacts_relations_id列也类似

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM