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

希望有人能帮我开始。


当前回答

在我的情况下,我只是改变了手动执行迁移的顺序,因此首先创建表用户。

在文件夹database/migrations/你的迁移文件名有这样的格式: year_month_day_hhmmss_create_XXXX_table.php

只是重命名创建用户文件,这样你的表优先级表的创建日期设置晚于用户日期(甚至晚一秒就足够了)

其他回答

问题已经回答了,但希望这能帮助到其他人。

这个错误发生在我身上,因为我首先在迁移表中创建了外键,然后该键才作为主键存在于它的原始表中。按照运行migrate:make后生成的文件名所指示的顺序执行迁移。例如2014年_05_10_165709_create_student_table.php。

解决方案是将具有外键的文件重命名为比具有主键的文件更早的时间,如本文所推荐的:http://forumsarchive.laravel.io/viewtopic.php?id=10246

我想我还必须添加$table->engine = 'InnoDB';

我用Laravel 5做数据透视表时也犯了同样的错误,我的问题是我没有

->删除('瀑布');

分两步添加它,最好让它也是无符号的:

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

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

}

它也可能是你创建迁移的顺序。如果你先创建优先级表,然后再创建用户表,那么它将是错误的。因为先迁移查找用户表。所以,你必须改变迁移的顺序

app/database/migrations

目录

你应该这样写

public function up()
{
    Schema::create('transactions', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->float('amount', 11, 2);
        $table->enum('transaction type', ['debit', 'credit']);
        $table->bigInteger('customer_id')->unsigned();      
        $table->timestamps();                 
    });

    Schema::table('transactions', function($table) {
        $table->foreign('customer_id')
              ->references('id')->on('customers')
              ->onDelete('cascade');
    });     
}

外键字段应不签名,希望对您有帮助!!