在 Laravel 5.4 中将外键 bigInteger 设置为 bigIncrements

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42442498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:24:41  来源:igfitidea点击:

Setting a foreign key bigInteger to bigIncrements in Laravel 5.4

phpmysqllaravellaravel-5

提问by Marcus Lee

So I'm trying to set a foreign key in my migrate file for laravel so the user table is simple but I'm trying to use bigIncrements instead of stand increments as such.

所以我试图在我的 Laravel 迁移文件中设置一个外键,所以用户表很简单,但我试图使用 bigIncrements 而不是这样的立场增量。

 public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->bigIncrements('id')->unsigned();
        $table->string('user_id')->unique();
        $table->string('avatar');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password')->nullable();
        $table->rememberToken();
        $table->timestampsTz();
    });
}

And when I do the other table I try to add foreign key to it I get a error saying the the foreign key is incorrectly formed. I'm confused as to how because I'm matching the column types. Here is the other table.

当我做另一个表时,我尝试向它添加外键,我收到一个错误,说外键的格式不正确。我对如何匹配感到困惑,因为我正在匹配列类型。这是另一张桌子。

public function up()
{
    Schema::create('social_logins', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->unsigned()->index();
        $table->string('provider', 32);
        $table->string('provider_id');
        $table->string('token')->nullable();
        $table->string('avatar')->nullable();
        $table->timestamps();
    });
}

回答by Alexey Mezenin

Remove unsigned()from:

unsigned()从以下位置移除:

$table->bigIncrements('id')->unsigned();

And the other migration should look like this:

另一个迁移应如下所示:

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

回答by Claudio Ludovico Panetta

The problem is that bigIncrementsreturns an unsignedBigInteger. In order to work the Foreign key must be an unsigned big integer with index().

问题是bigIncrements返回一个 unsignedBigInteger。为了工作外键必须是一个无符号的大整数index()

回答by Afraz Ahmad

Reason is primaryand foreignreferences must be of same type.

原因是引用和外部引用必须是同一类型。

bigIncrements()wants unsignedBigInteger()

bigIncrements()想要 unsignedBigInteger()

and increments()wants unsignedInteger()

并且increments()想要unsignedInteger()

If you are using

如果您正在使用

$table->bigIncrements('id');as a primary key in a users table:

$table->bigIncrements('id');作为用户表中的主键:

then use

然后使用

$table->unsignedBigInteger('user_id'); as foreign key

And if you are using $table->increments('id');as a primary key in a users table then use

如果您$table->increments('id');在用户表中用作主键,则使用

$table->unsignedInteger('user_id'); as freign key.



$table->increments(); creates just integer and $table->bigIncrements(); creates big integer. 

so the reference type must be the same.

所以引用类型必须相同。

Read more https://laravel.com/docs/4.2/schema#adding-columns

阅读更多https://laravel.com/docs/4.2/schema#adding-columns