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

希望有人能帮我开始。


当前回答

这么简单!!

如果您第一次创建“priorities”迁移文件,Laravel首先运行“priorities”而“users”表不存在。

它如何向一个不存在的表添加关系!

解决方案:从“优先级”表中取出外键代码。你的迁移文件应该是这样的:

并添加到一个新的迁移文件,这里它的名字是create_prioritiesForeignKey_table,并添加这些代码:

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

其他回答

这么简单!!

如果您第一次创建“priorities”迁移文件,Laravel首先运行“priorities”而“users”表不存在。

它如何向一个不存在的表添加关系!

解决方案:从“优先级”表中取出外键代码。你的迁移文件应该是这样的:

并添加到一个新的迁移文件,这里它的名字是create_prioritiesForeignKey_table,并添加这些代码:

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

在自动创建的create_users_table迁移更改$table->id();表- >增加美元(id);。为我做了这个技巧,希望这对你有帮助!

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

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

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

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

(学习英语,不好意思) 我尝试在我的项目与“foreignId”和工作。在你的代码中只是删除列user_id,并在引用上添加foreignId:

 public function up()
{

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

IMPORTANTE:在本例中,首先创建没有外键的表,即“users”表

快捷的解决方案:

/ /内部模式::创建

表- >外国(user_id) -先导入bigInteger (user_id) - > >无符号()- >索引();

/ /内部模式::表

$table->foreign('users_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');

//正在编写代码。

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

  Schema::table('administrador', function($table) {
            $table->foreign('users_id')->references('id')->on('users')
            ->onUpdate('cascade')
            ->onDelete('cascade');
;
        });

}