我试图在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等。

希望有人能帮我开始。


当前回答

另一个原因可能是DB引擎类型。Laravel默认使用MyISAM引擎。所以如果你需要强制外键引用检查,你应该使用InnoDB作为你的引擎。

这个设置在config/database.php文件中。将引擎改为'engine' => 'InnoDB'

另外,如果您试图对现有表执行此操作,请使用

DB::statement("ALTER TABLE ' {$tableName} ' ENGINE = 'InnoDB'");

其他回答

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

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');
    });

你可以直接在整型列中传递布尔参数,说它是否应该是无符号的。在laravel 5.4下面的代码解决了我的问题。

        $table->integer('user_id', false, true);

这里第二个参数false表示它不应该是自动递增的,第三个参数true表示它应该是无符号的。可以将外键约束保留在同一迁移中,也可以将其分离。两者都适用。

上面所有的解决方案都是正确的,但没有一个适合我。

为我创造问题的东西是数据库表引擎。引擎类型已经添加的表是MyISAM和新的迁移正在设置引擎类型= 'InnoDB'

我只是设置引擎类型已经添加表到InnoDB,就是这样!

为了在laravel中添加外键约束,我使用了以下方法:

创建列为外键,如下所示: 表- >美元整数(column_name) - >无符号(); 在(1)之后立即添加约束行,即。 表- >美元整数(column_name) - >无符号(); 表- >外国(column_name) - >引用(pk_of_other_table) - >(“other_table”);

在我的情况下,直到我运行命令它才工作

composer dump-autoload

这样你就可以把外键留在create Schema中

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');
}