【问题标题】:Migration: Cannot add foreign key constraint迁移:无法添加外键约束
【发布时间】:2014-05-02 04:40:30
【问题描述】:

我正在尝试在 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_projectproject_tasks 等。

希望有人可以帮助我开始。

【问题讨论】:

标签: laravel laravel-4 foreign-keys migration eloquent


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    对我来说,我的子表引用的表列没有被索引。

    Schema::create('schools', function (Blueprint $table) {
        $table->integer('dcid')->index()->unque();
        $table->integer('school_number')->index(); // The important thing is that this is indexed
        $table->string('name');
        $table->string('abbreviation');
        $table->integer('high_grade');
        $table->integer('low_grade');
        $table->timestamps();
        $table->primary('dcid');
    });
    
    Schema::create('students', function (Blueprint $table) {
          $table->increments('id');
          $table->integer('dcid')->index()->unique()->nullable();
          $table->unsignedInteger('student_number')->nullable();
          $table->integer('schoolid')->nullable();
          $table->foreign('schoolid')->references('school_number')->on('schools')->onDelete('set null');
          // ...
    });
    

    忽略糟糕的命名,它来自另一个设计糟糕的系统。

    【讨论】:

      【解决方案3】:

      有时由于迁移顺序可能会出现此错误。

      就像用户和订单是两个表

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

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

      示例: 在我的情况下,教育和大学表 教育表

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

      【讨论】:

        【解决方案4】:

        在自动创建的 create_users_table 迁移中将$table->id(); 更改为$table->increments('id');。为我做了诀窍,希望这会有所帮助!

        【讨论】:

          【解决方案5】:

          捷径解决方案:

          //内部架构::create

          $table->foreign('user_id')->bigInteger('user_id')->unsigned()->index();

          //Schema::table 内部

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

          【讨论】:

            【解决方案6】:

            我认为这里的答案中缺少一件事,如果我错了,请纠正我,但外键需要在数据透视表上建立索引。至少在 mysql 中似乎是这样的。

            public function up()
            {
                Schema::create('image_post', function (Blueprint $table) {
                    $table->engine = 'InnoDB';
                    $table->increments('id');
                    $table->integer('image_id')->unsigned()->index();
                    $table->integer('post_id')->unsigned()->index();
                    $table->timestamps();
                });
            
                Schema::table('image_post', function($table) {
                    $table->foreign('image_id')->references('id')->on('image')->onDelete('cascade');
                    $table->foreign('post_id')->references('id')->on('post')->onDelete('cascade');
                });
            
            }
            

            【讨论】:

              【解决方案7】:

              在制作数据透视表时,我在使用 Laravel 5 时遇到了同样的错误,而我的问题是我没有

              ->onDelete('cascade');

              【讨论】:

                【解决方案8】:

                我认为:引用键必须是“索引”。 例如:(下)

                public function up()
                {
                    Schema::create('clicks', function (Blueprint $table) {
                        $table->increments('id');
                        $table->string('viewer_id');
                        $table->integer('link_id')->index()->unsigned();
                        $table->string('time');
                        $table->timestamps();
                    });
                
                    Schema::table('clicks', function($table) {
                        $table->foreign('link_id')->references('id')->on('links')->onDelete('cascade');
                    });
                
                
                }
                

                祝你好运。

                【讨论】:

                  【解决方案9】:

                  请先检查列的数据类型。如果第一个表 id 类型是 bigint,那么在添加外键时检查其他表中的类型应该是 bigint。

                  【讨论】:

                    【解决方案10】:

                    上述所有解决方案在某种程度上都是正确的,但没有一个对我有用。

                    给我带来问题的是数据库表引擎。已添加表的引擎类型为 MyISAM,新的 migration 设置为 engine type = 'InnoDB'

                    我刚刚将 Engine Type 的已添加表设置为 InnoDB ,就是这样!

                    【讨论】:

                      猜你喜欢
                      • 2019-03-24
                      • 2017-03-27
                      • 2021-08-30
                      • 2020-09-21
                      • 2020-03-12
                      • 2020-01-20
                      • 2019-07-31
                      相关资源
                      最近更新 更多