Php 在Laravel 5.4中将外键bigInteger设置为bigIncrements
因此,我试图在我的迁移文件中为laravel设置一个外键,这样用户表很简单,但我尝试使用bigIncrements,而不是stand incrementsPhp 在Laravel 5.4中将外键bigInteger设置为bigIncrements,php,mysql,laravel,laravel-5,Php,Mysql,Laravel,Laravel 5,因此,我试图在我的迁移文件中为laravel设置一个外键,这样用户表很简单,但我尝试使用bigIncrements,而不是stand increments public function up() { Schema::create('users', function (Blueprint $table) { $table->engine = 'InnoDB'; $table->bigIncrements('id')->unsigned(
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();
});
}
当我在另一个表中添加外键时,我会得到一个错误,表示外键的格式不正确。我对如何匹配感到困惑,因为我正在匹配列类型。这是另一张桌子
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();
});
}
问题是
bigingress
返回一个无符号的biginger。
为了工作,外键必须是带有索引()
的无符号大整数,从以下位置删除无符号()
:
$table->bigIncrements('id')->unsigned();
另一个迁移应该如下所示:
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
....
$table->index('user_id');
原因是主要和外部引用必须是同一类型
bigingress()
想要unsignedbiginger()
和increments()
想要unsignedInteger()
如果您正在使用
$table->bigIncrements('id')代码>作为用户表中的主键:
然后使用
$table->unsignedBigInteger('user_id'); as foreign key
如果您使用的是$table->increments('id')代码>作为用户表中的主键,然后使用
$table->unsignedInteger('user_id'); as freign key.
$table->increments(); creates just integer and $table->bigIncrements(); creates big integer.
因此,引用类型必须相同
阅读更多内容尝试删除外键语句末尾的->unsigned()->index()
非常感谢您这么做。或者简单地使用unsignedbiginger