Setting a foreign key bigInteger to bigIncrements

2019-05-07 02:16发布

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.

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

2条回答
叼着烟拽天下
2楼-- · 2019-05-07 02:55

Remove unsigned() from:

$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');
查看更多
我命由我不由天
3楼-- · 2019-05-07 03:11

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

查看更多
登录 后发表回答