错误问题:1215无法添加外键约束

vktxenjb  于 2021-07-29  发布在  Java
关注(0)|答案(3)|浏览(391)

如何解决这个错误?sqlstate[hy000]:常规错误:1215无法添加外键约束(sql:alter table) invoices 添加约束 invoices_form_id_foreign 外键( form_id )参考文献 forms ( id )删除时(级联)
我在php artisan之后得到它migrate:fresh
对于发票中的外键,获取此错误。3个外键生成错误,但一个用于用户id的外键正常工作。我尝试了所有的解决方案,但都没有成功。请帮帮我。
2014\ 10\ 12\ 000000\创建\用户\表格.php

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('surname')->nullable();
            $table->string('showname')->nullable();
            $table->string('business')->nullable();
            $table->string('NIP')->nullable();
            $table->string('PESEL')->nullable();
            $table->string('address')->nullable();
            $table->string('city')->nullable();
            $table->string('postalcode')->nullable();
            $table->string('phone')->nullable();
            $table->string('comments')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

2020\ U 07\ U 23\ U 104440\ U发票.php

<?php

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

class Invoices extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('invoices', function (Blueprint $table) {
            $table->increments('id');
            $table->string('invoicenumber')->nullable();
            $table->date('invoicedate')->nullable();
            $table->date('selldate')->nullable();
            $table->integer('user_id')->unsigned()->nullable();
            $table->integer('form_id')->unsigned()->nullable();                        
            $table->integer('currency_id')->unsigned()->nullable();

            $table->integer('proform_id')->unsigned()->nullable(); 
            $table->string('paymentmethod')->nullable();
            $table->date('paymentdate')->nullable();
            $table->string('status')->nullable();
            $table->string('comments')->nullable();
            $table->string('city')->nullable();
            $table->string('paid')->nullable();
            $table->string('autonumber')->nullable();
            $table->string('automonth')->nullable();
            $table->string('autoyear')->nullable();
            $table->timestamps();
        });

        Schema::table('invoices', function (Blueprint $table){
            $table->foreign('user_id')
                  ->references('id')
                  ->on('users');

            $table->foreign('form_id')
                  ->references('id')
                  ->on('forms');

            $table->foreign('currency_id')
                  ->references('id')
                  ->on('currencys');

            $table->foreign('proform_id')
                  ->references('id')
                  ->on('proforms');

        });

    }

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

2020\u 07\u 27\u 090356\u proforms.php

<?php

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

class Proforms extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('proforms', function (Blueprint $table) {
            $table->increments('id');
            $table->string('proformnumber')->nullable();
            $table->date('proformdate')->nullable();
            $table->date('selldate')->nullable();
            $table->integer('user_id')->unsigned()->nullable();
            $table->string('paymentmethod')->nullable();
            $table->date('paymentdate')->nullable();
            $table->string('status')->nullable();
            $table->string('comments')->nullable();
            $table->timestamps();
        });

        Schema::table('proforms', function (Blueprint $table){
            $table->foreign('user_id')
                  ->references('id')
                  ->on('users');
        });

    }

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

2020\ u 07\ u 28\ u 091856\ u forms.php

<?php

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

class Forms extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('forms', function (Blueprint $table) {
            $table->increments('id');
            $table->string('form')->nullable();
            $table->timestamps();
        });
    }

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

2020\u 07\u 28\u 091919\u currencys.php

<?php

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

class Currencys extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('currencys', function (Blueprint $table) {
            $table->increments('id');
            $table->string('currency')->nullable();
            $table->string('course')->nullable();
            $table->timestamps();
        });
    }

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

muk1a3rh1#

下面是迁移过程中可能发生的情况。
1.检查主键和外键类型。如果用户主键id为类型 $table->increments('id'); // bigInteger ,则外键也应为biginteger $table->bigInteger('user_id') ;
2.请检查您的迁移顺序。例如表格 invoices 具有来自的外键 forms table . 在迁移过程中, forms 应首先迁移表。为了实现这一点,只需重命名迁移文件(文件名的数字部分)就可以了。

zz2j4svz

zz2j4svz2#

因为在执行迁移时到达这条线:

$table->foreign('form_id')->references('id')->on('forms');

在2020年\u 07 \u 23 \u 104440 \u发票文件中,尚未创建表单表
你应该进行一次新的迁移来建立这种关系

Schema::table('invoices', function (Blueprint $table){ 
            $table->foreign('form_id')
                  ->references('id')
                  ->on('forms');
}

此迁移应该在2020年之后\u 07 \u 28 \u 091856 \u forms.php迁移。。。

0pizxfdo

0pizxfdo3#

问题是迁移的顺序
请看订单

2020_07_23_104440_invoices.php

2020_07_28_091856_forms.php

当创建发票表时,将不会有表单表,因为它是随后创建的。所以这个错误是因为您在创建invoices表时试图在不存在的forms表id字段之间创建关系。
您可以做的是创建一个新的迁移文件,仅用于附加关系。
下面是如何做删除添加外键从。从2020\u 07\u 23\u 104440\u invoices.php中删除此部分

$table->foreign('form_id')
                      ->references('id')
                      ->on('forms');

删除后,为添加外键创建单独的迁移
运行 php artisan make:migration add_form_foreign_key_to_invoices 它将生成新的迁移文件。像这样更新。

<?php

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

class AddFormForeignKeyToInvoices extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('invoices', function (Blueprint $table) {
            $table->foreign('form_id')->references('id')->on('forms');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('invoices', function (Blueprint $table) {
            $table->dropForeign('form_id');
        });
    }
}

相关问题