Mar*_*Lee 6 php mysql laravel laravel-5
因此,我试图在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();
});
}
Run Code Online (Sandbox Code Playgroud)
当我执行另一个表时,我尝试向其中添加外键,但我收到一条错误消息,指出外键的格式不正确。我对方法感到困惑,因为我要匹配列类型。这是另一张桌子。
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();
});
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*nin 17
unsigned()从以下位置删除:
$table->bigIncrements('id')->unsigned();
Run Code Online (Sandbox Code Playgroud)
其他迁移应如下所示:
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
....
$table->index('user_id');
Run Code Online (Sandbox Code Playgroud)
原因是主要的,国外参考文献必须是同一类型。
bigIncrements()想要unsignedBigInteger()
并increments()想要unsignedInteger()
如果您正在使用
$table->bigIncrements('id');作为用户表中的主键:
然后使用
$table->unsignedBigInteger('user_id'); as foreign key
Run Code Online (Sandbox Code Playgroud)
如果您$table->increments('id');在用户表中用作主键,则使用
$table->unsignedInteger('user_id'); as freign key.
$table->increments(); creates just integer and $table->bigIncrements(); creates big integer.
Run Code Online (Sandbox Code Playgroud)
所以引用类型必须相同。
了解更多https://laravel.com/docs/4.2/schema#adding-columns