duanji4870
2019-03-13 09:12
浏览 221
已采纳

Laravel:外键错误形成

So I am busy with making this migration and afther a ton of research, he still gives me the following error message:

SQLSTATE[HY000]: General error: 1005 Can't create table dev.bindings (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table bindings add constraint bindings_users_id_foreign foreign key (users_id) references users (id) on delete cascade on update cascade)

Code:

Schema::create('bindings', function (Blueprint $table) {
    $table->unsignedInteger('users_id');

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

Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
});

Versions:

PHP: v7.3
Laravel: v5.8.4
MariaDB: v10.3.13
Homestead: v8.1.0

As that I am aware, this should form a foreign key correctly. Any help would be appriciated!

图片转代码服务由CSDN问答提供 功能建议

所以我忙于进行此迁移并进行大量研究,他仍然给出了以下错误消息:

SQLSTATE [HY000]:常规错误:1005无法创建表 dev bindings (错误号: 150“外键约束形成错误”)(SQL:alter table bindings 添加约束 bindings_users_id_foreign 外键( users_id )引用用户 id )关于更新级联上的删除级联)

代码:

  Schema :: create('bindings',function(Blueprint $ table){
 $ table-> unsignedInteger('users_id'); 
 
 $ table-> foreign('users_id')\  n  - >引用('id') - > on('users')
  - > onDelete('cascade')
  - > onUpdate('cascade'); 
}); 
 \  nSchema :: create('users',function(Blueprint $ table){
 $ table-> bigIncrements('id'); 
}); 
   
 
 <  p>版本 s: 
 
 
  PHP:v7.3 
Laravel:v5.8.4 
MariaDB:v10.3.13 
Homestead:v8.1.0 
   \  n 
 

正如我所知,这应该正确形成一个外键。 任何帮助都会受到欢迎!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doubi3929 2019-03-13 09:25
    已采纳

    Update: as other answers stated, i did not notice it, you will need to create the users table, before the foreign key can be created.

    When you do foreign keys, both fields will need to have the same properties. So in your case if you use bigIncrements your users_id needs to be an unsigned big int.

    // User table needs to be created first
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
    });
    
    Schema::create('bindings', function (Blueprint $table) {
        $table->bigInteger('users_id')->unsigned();
    
        ...
    });
    

    Also the standard is to name it user_id, this will make it easier to do relations in Laravel.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongzhuanlei0768 2019-03-13 09:29

    In your migration, place your parent table migration on top then child table

        Schema::create('users', function (Blueprint $table) {
                $table->bigIncrements('id');
            });
    
    
    Schema::create('bindings', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id');
    
            $table->foreign('user_id')
                ->references('id')->on('users')
                ->onDelete('cascade')
                ->onUpdate('cascade');
        });
    

    And then run php artisan migrate command.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题