我试图在Laravel中创建外键,但是当我使用artisan迁移我的表时,我抛出了以下错误:

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `priorities` add constraint priorities_user_id_foreign foreign 
key (`user_id`) references `users` (`id`))     

我的迁移代码如下:

优先级迁移文件

public function up()
{
    //
    Schema::create('priorities', function($table) {
        $table->increments('id', true);
        $table->integer('user_id');
        $table->foreign('user_id')->references('id')->on('users');
        $table->string('priority_name');
        $table->smallInteger('rank');
        $table->text('class');
        $table->timestamps('timecreated');
    });
}

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

用户迁移文件

public function up()
{
    //
    Schema::table('users', function($table)
    {
    $table->create();
    $table->increments('id');
    $table->string('email');
    $table->string('first_name');
    $table->string('password');
    $table->string('email_code');
    $table->string('time_created');
    $table->string('ip');
    $table->string('confirmed');
    $table->string('user_role');
    $table->string('salt');
    $table->string('last_login');

    $table->timestamps();
    });
}

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

关于我做错了什么,我现在想要得到这个,因为我有很多表,我需要创建例如用户,客户端,项目,任务,状态,优先级,类型,团队。理想情况下,我想创建表持有这些数据与外键,即clients_project和project_tasks等。

希望有人能帮我开始。


当前回答

有时这种错误可能是由于迁移的顺序造成的。

Users和Order是两个表

订单表有用户的外键 (在迁移过程中,如果Order表先迁移,则会导致问题,因为没有用户匹配外键)

解决方案: 只需将您的Order Update表放在用户下面进行更新

例子: 在我的例子中,教育和大学表 教育表

public function up()
{
    Schema::create('doc_education', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('uni_id')->unsigned()->nullable();
        $table->timestamps();
    });
}

在大学里

    Schema::create('doc_universties', function (Blueprint $table) {
        $table->increments('id');
        $table->string('uni_name');
        $table->string('location')->nullable();
        $table->timestamps();

        //
    });



Schema::table('doc_education', function(Blueprint $table) {
        $table->foreign('uni_id')->references('id')
        ->on('doc_universties')->onDelete('cascade');
    });

其他回答

有时这种错误可能是由于迁移的顺序造成的。

Users和Order是两个表

订单表有用户的外键 (在迁移过程中,如果Order表先迁移,则会导致问题,因为没有用户匹配外键)

解决方案: 只需将您的Order Update表放在用户下面进行更新

例子: 在我的例子中,教育和大学表 教育表

public function up()
{
    Schema::create('doc_education', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('uni_id')->unsigned()->nullable();
        $table->timestamps();
    });
}

在大学里

    Schema::create('doc_universties', function (Blueprint $table) {
        $table->increments('id');
        $table->string('uni_name');
        $table->string('location')->nullable();
        $table->timestamps();

        //
    });



Schema::table('doc_education', function(Blueprint $table) {
        $table->foreign('uni_id')->references('id')
        ->on('doc_universties')->onDelete('cascade');
    });

我注意到的一件事是,如果表使用不同的引擎比外键约束不起作用。

例如,如果一个表使用:

$table->engine = 'InnoDB';

还有其他用途

$table->engine = 'MyISAM';

会产生一个错误:

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

你可以通过在表创建的最后添加InnoDB来解决这个问题,如下所示:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedInteger('business_unit_id')->nullable();

        $table->string('name', 100);

        $table->foreign('business_unit_id')
                ->references('id')
                ->on('business_units')
                ->onDelete('cascade');

        $table->timestamps();
        $table->softDeletes();
        $table->engine = 'InnoDB'; # <=== see this line
    });
}

在我的例子中,问题是主表中已经有记录,我强制新列不为NULL。因此,在新列中添加->nullable()就成功了。在这个问题的例子中是这样的:

表- >美元整数(user_id) - >无符号()——> nullable ();

or:

表- > unsignedInteger (user_id) - > nullable ();

希望这能帮助到一些人!

在我的案例中,问题在于迁移时机,在创建迁移时要小心,首先创建子迁移,而不是基础迁移。因为如果你先创建base migration,有外键就会寻找子表,不会有抛出异常的表。

进一步:

当您创建迁移时,它的开头有一个时间戳。假设你已经创建了一个迁移猫,它看起来像2015_08_19_075954_the_cats_time.php,它有这样的代码

<?php

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

class TheCatsTime extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cat', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');  
            $table->date('date_of_birth');
            $table->integer('breed_id')->unsigned()->nullable(); 
        });

        Schema::table('cat', function($table) {
        $table->foreign('breed_id')->references('id')->on('breed');
      });
    }

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

在创建基表之后,创建另一个迁移品种,即子表,它有自己的创建时间和日期戳。 代码如下所示:

<?php

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

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

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

看起来这两个表都是正确的,但当你运行PHP artisan迁移。它会抛出一个异常,因为迁移会首先在你的数据库中创建基表因为你先创建了这个迁移,我们的基表有外键约束,它会寻找子表,而子表不存在,这可能是一个异常。

So:

首先创建子表迁移。 在创建子迁移之后创建基表迁移。 PHP工匠迁移。

做就行了

在laravel 5.8中,users_table使用bigIncrements('id')数据类型作为主键。所以当你想引用一个外键约束时,你的user_id列需要是unsignedBigInteger('user_id')类型。