【问题标题】:SQLSTATE[HY000]: General error: 1005 Can't create table - Laravel 4SQLSTATE [HY000]:一般错误:1005 无法创建表 - Laravel 4
【发布时间】:2014-01-26 18:15:29
【问题描述】:

我在执行 php artisan 迁移时收到此错误。我的迁移文件有问题吗?还是我的模型编码错误?但是即使模型中有问题,迁移也应该可以工作?

[Exception]                                                                  
  SQLSTATE[HY000]: General error: 1005 Can't create table 'festival_aid.#sql-  
  16643_2033' (errno: 150) (SQL: alter table `gigs` add constraint gigs_band_  
  id_foreign foreign key (`band_id`) references `bands` (`band_id`) on delete  
   cascade) (Bindings: array (                                                 
  ))

[PDOException]                                                               
  SQLSTATE[HY000]: General error: 1005 Can't create table 'festival_aid.#sql-  
  16643_2033' (errno: 150)

演出迁移

public function up()
    {
        Schema::create('gigs', function($table)
        {
            $table->increments('gig_id');

            $table->dateTime('gig_startdate');

            $table->integer('band_id')->unsigned();
            $table->integer('stage_id')->unsigned();

            $table->foreign('band_id')
                ->references('band_id')->on('bands')
                ->onDelete('cascade');

            $table->foreign('stage_id')
                ->references('stage_id')->on('stages')
                ->onDelete('cascade');
        });

    public function down()
    {
        Schema::table('gigs', function($table)
        {
            Schema::drop('gigs');
            $table->dropForeign('gigs_band_id_foreign');
            $table->dropForeign('gigs_stage_id_foreign');
        });
    }

乐队迁移

public function up()
    {
        Schema::create('bands', function($table)
        {
            $table->increments('band_id');

            $table->string('band_name');
            $table->text('band_members');
            $table->string('band_genre');
            $table->dateTime('band_startdate');
        });
    }

    public function down()
    {
        Schema::table('bands', function(Blueprint $table)
        {
            Schema::drop('bands');
        });
    }

模型乐队

<?php

class Band extends Eloquent {

    protected $primaryKey = 'band_id';

    public function gig()
    {
        return $this->hasOne('Gig', 'band_id', 'band_id');
    }
}

模型演出

<?php

class Gig extends Eloquent {
    protected $primaryKey = 'gig_id';

    public function gig()
    {
        return $this->belongsTo('Band', 'band_id', 'band_id');
    }

    public function stage()
    {
        return $this->belongsTo('Stage', 'stage_id', 'stage_id');
    }
}

【问题讨论】:

    标签: php laravel migration laravel-4


    【解决方案1】:

    必须先创建表,再创建外键:

    Schema::create('gigs', function($table)
    {
        $table->increments('gig_id');
    
        $table->dateTime('gig_startdate');
    
        $table->integer('band_id')->unsigned();
        $table->integer('stage_id')->unsigned();
    });
    
    Schema::table('gigs', function($table)
    {
        $table->foreign('band_id')
            ->references('band_id')->on('bands')
            ->onDelete('cascade');
    
        $table->foreign('stage_id')
            ->references('stage_id')->on('stages')
            ->onDelete('cascade');
    });
    

    您的bands 表应该首先迁移,因为gigs 正在引用它。

    【讨论】:

    • 好的,我这样做了:) 仍然得到同样的错误。我用我的模型更新了我的问题。
    • 乐队迁移了吗?还是没有?乐队应该首先迁移,因为 gigs 会引用它。
    • 就是这样。我更改了迁移文件的前缀。
    • 是的,你需要检查迁移文件的顺序
    • 我不知道这是否是一个旧要求,但从 5.0 开始,您可以在创建回调中包含外部命令。
    【解决方案2】:

    虽然这不适用于 OP,但其他人可能会遇到此问题:

    从底部Laravel Schema docs

    注意:创建引用递增整数的外键时,请记住始终使外键列无符号。

    在迁移文件中创建表时,您可以通过$table-&gt;integer('user_id')-&gt;unsigned(); 执行此操作。

    我花了几分钟才意识到发生了什么。

    【讨论】:

    • 这就是答案,不知道这个魔术贴怎么不流行。
    【解决方案3】:

    对于那些其他答案没有帮助的人,当您尝试在不可为空的列上使用 'SET_NULL' 操作时也会引发相同的错误。

    【讨论】:

      【解决方案4】:

      正如Andrew 所说,将表格上的引用设为:

      $table->integer('user_id')->unsigned();
      

      它应该可以工作。

      【讨论】:

        【解决方案5】:

        这似乎是一般的外键问题错误。对我来说,当我切换 referenceson 方法中的参数时,我得到了它。我有:

        $table->foreign('user_id')->references('users')->on('id');
        

        代替:

        $table->foreign('user_id')->references('id')->on('users');
        

        【讨论】:

          【解决方案6】:

          我刚刚解决了这个问题,通过使用unique() 指向 Laravel 该字段是唯一的。

          例子:

          Schema::create('branches', function (Blueprint $table) {
                      $table->increments('id');
                      /* This fields serves as foriegn key on functions 
                      table and it must follow the database **key rules**
                      by being unique */
                      $table->string('branch_code')->unique();
                      $table->string('branch_name');
                      $table->string('branch_address');
                      $table->timestamps();
                  });
          

          功能表:

              Schema::create('functions', function (Blueprint $table) {
                  $table->increments('id');
                  $table->string('branch_code');
                  $table->string('function');
                  $table->timestamps();
                  //Reference to branch_code on branches table
                  $table->foreign('branch_code')->references('branch_code')->on('branches');
          
              });
          

          【讨论】:

            【解决方案7】:

            我将配置/数据库中的 'engine' =&gt; 'InnoDB', 更改为 'engine' =&gt; null, 并且可以正常工作

            【讨论】:

              【解决方案8】:

              对于那些仍然遇到此问题的人,请确保您设置为外键的字段是主键或应该是唯一的,因为外键只能是主字段或唯一字段。 请参阅下面的示例

              Schema::create('users', function (Blueprint $table) {
                $table->increments('id');
                $table->string('username',25)->unique();
               });
              
              Schema::create('another_table', function (Blueprint $table) {
                 $table->increments('id');
                 $table->string('name', 25);
                 $table->foreign('name')->references('username')->on('users')
               });
              

              【讨论】:

                【解决方案9】:
                        $table->integer('band_id')->unsigned();
                        $table->integer('stage_id')->unsigned();
                

                在 laravel 5.8 中,users_table 使用 bigIncrements('id') 数据类型作为主键。因此,当您要引用外键约束时,您的 band_id,stage_id 列需要是 unsignedBigInteger('stage_id') 和 band_id 类型。

                经理也这样测试过。

                【讨论】:

                  【解决方案10】:

                  你可以使用

                  $table->bigInteger('user_id')->unsigned();
                  $table->foreign('user_id')->references('users')->on('id');
                  

                  BigInt 的数据类型为 bigint(20),而整数的数据类型为 int(10)。虽然这两个都是无符号整数,但“长度/值”不匹配。

                  【讨论】:

                    【解决方案11】:

                    对于 Laravel 6.X,使用这种格式。

                    $table->unsignedBigInteger('dest_id')->unsigned();
                    
                    $table->foreign('dest_id')->references('id')->on('destinations')->onDelete('cascade');
                    

                    【讨论】:

                      【解决方案12】:

                      以上没有对我有用! 但是这样做了:我只是将整数更改为 unsignedInteger

                      $table->unsignedBigInteger('user_id')->unsigned();
                      

                      【讨论】:

                        【解决方案13】:

                        更改自:

                        $table->unsignedInteger('user_id')->unique();
                        

                        收件人:

                        $table->unsignedBigInteger('user_id')->unique();
                        

                        为我工作

                        【讨论】:

                          【解决方案14】:

                          外键必须使用 unsignedInterger 或 unsignedBigInterger

                          考试:

                          public function up()
                          {
                              Schema::create('likes', function (Blueprint $table) {
                                  $table->increments('id');
                                  $table->unsignedInteger('user_id');
                                  $table->unsignedInteger('post_id');
                                  $table->string('post_type');
                                  $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
                                  $table->timestamps();
                              });
                          }
                          

                          【讨论】:

                            【解决方案15】:

                            我遇到了类似的问题。 你必须使用 bigInteger 来解决这个问题:

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

                            【讨论】:

                              【解决方案16】:

                              您还可以为约束的“on delete”和“on update”属性指定所需的操作:

                              $table->foreignId('user_id')
                                ->constrained()
                                ->onUpdate('cascade')
                                ->onDelete('cascade');
                              

                              【讨论】:

                                猜你喜欢
                                • 2021-03-20
                                • 1970-01-01
                                • 2020-05-26
                                • 1970-01-01
                                • 2016-12-02
                                • 2021-06-16
                                • 2019-11-14
                                • 2020-12-19
                                • 2021-09-20
                                相关资源
                                最近更新 更多