雄辩 - 无法添加外键约束

问题描述:

我猜想的常见问题,但我无法解决它,尽管我在网上找到的信息。雄辩 - 无法添加外键约束

我有关系的hasMany的序列:

  • 用户有许多客户有许多合同有许多材料

我做了我最好的,但我发现自己面对这个错误:

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: create table clients (id int unsigned not null, user_id int unsigne d null, ...) default character set utf8mb4 collate
utf8mb4_unicode_ci engine = InnoDB)

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

迁移:

class CreateUsersTable extends Migration 
{ 
    public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
      $table->engine = 'InnoDB'; 

      // Keys 

      $table->increments('id'); 

      // Other 

      ... 
      $table->timestamps(); 
     }); 
    } 

    public function down() 
    { 
     Schema::disableForeignKeyConstraints(); 
     Schema::dropIfExists('clients'); 
     Schema::drop('users'); 
     Schema::enableForeignKeyConstraints(); 
    } 
} 

class CreateClientsTable extends Migration 
{ 
    public function up() 
    { 
     Schema::create('clients', function (Blueprint $table) { 
      $table->engine = 'InnoDB'; 

      // keys 

      $table->unsignedInteger('id')->unique(); 
      $table->primary('id'); 

      $table->unsignedInteger('user_id') 
        ->nullable(); 
      $table->foreign('user_id')->references('id')->on('users') 
        ->onDelete('cascade'); 

      // others 
      ... 
     }); 
    } 

    public function down() 
    { 
     Schema::disableForeignKeyConstraints(); 
     Schema::dropIfExists('contracts'); 
     Schema::drop('clients'); 
     Schema::enableForeignKeyConstraints(); 
    } 
} 

class CreateContractsTable extends Migration 
{ 
    public function up() 
    { 
     Schema::create('contracts', function (Blueprint $table) { 
      $table->engine = 'InnoDB'; 

      // Keys 

      $table->increments('id'); 
      $table->string('contract_number')->unique(); 

      $table->unsignedInteger('client_id'); 
      $table->foreign('client_id')->references('id')->on('clients') 
        ->onDelete('cascade'); 


      // Others 

      ... 
     }); 
    } 

    public function down() 
    { 
     Schema::disableForeignKeyConstraints(); 
     Schema::dropIfExists('materials'); 
     Schema::drop('contracts'); 
     Schema::enableForeignKeyConstraints(); 
    } 
} 

class CreateMaterialsTable extends Migration 
{ 
    public function up() 
    { 
     Schema::create('materials', function (Blueprint $table) { 
      $table->engine = 'InnoDB'; 

      // Keys 

      $table->increments('id'); 

      $table->string('contract_number')->unique(); 

      $table->unsignedInteger('contract_id'); 
      $table->foreign('contract_id')->references('id')->on('contracts') 
        ->onDelete('cascade'); 

      // Others 

      ... 
     }); 
    } 

    public function down() 
    { 
     Schema::drop('materials'); 
    } 
} 

我在做什么错?

+0

你使用的是mysql/mariadb吗?如果是,什么版本? – Jerodev

+0

你的列是同一种数据类型吗? –

+0

我正在使用mysql,是的他们是... – Pixeuh

您应该将->unsigned()添加到您拥有的所有关键列中。 请使用此代替unsignedInteger()并将类型设置为integer()

+0

只是做,但显然不会改变任何... – Pixeuh

您确定您的迁移按正确的顺序运行吗?它们应该按照您在示例代码中提供的顺序运行。我似乎无法使用您的确切示例迁移重新创建我的最终错误。

订单取决于迁移文件名开头的日期。或者,您可以在同一迁移中创建所有这些表以强制执行该顺序。

+0

是的,我已经这样做.... – Pixeuh