Laravel表:只能有一个自动列,必须将其定义为键

Smi*_*ley 11 php mysql laravel

我已经使所有整数无符号但我仍然得到错误.我需要改变什么?

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFacebook extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
    Schema::create('facebook', function($table)
        {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
        $table->timestamps();
        $table->string('username', 255);
        $table->bigInteger('uid', 20)->unsigned();
        $table->string('access_token', 255);
        $table->string('access_token_secret', 255);
        $table->string('photoURL', 255);
        $table->string('profileURL', 255);
        $table->string('firstName', 255);
        $table->string('lastName', 255);
        $table->string('gender', 255);
        $table->string('age', 20);
        $table->integer('birthDay')->unsigned();
        $table->integer('birthMonth')->unsigned();
        $table->integer('birthYear')->unsigned();
        $table->string('email', 255);
        $table->string('phone', 30);
        $table->string('address', 255);
        $table->string('country', 100);
        $table->string('region', 100);
        $table->string('city', 100);
        $table->string('zip', 20);
        });
        }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('facebook');
    }

}
Run Code Online (Sandbox Code Playgroud)

SQLSTATE [42000]:语法错误或访问冲突:1075表定义不正确; 只能有一个自动列,必须将其定义为键

谢谢.

new*_*tol 28

使用

$table->bigInteger('uid')->unsigned();
Run Code Online (Sandbox Code Playgroud)

代替

$table->bigInteger('uid', 20)->unsigned();
Run Code Online (Sandbox Code Playgroud)

相关信息:

/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint你可以看到bigInteger函数:

public function bigInteger($column, $autoIncrement = false, $unsigned = false)
Run Code Online (Sandbox Code Playgroud)

所以20(不是0)逃避true.

虽然string方法有第二个参数length:

public function string($column, $length = null)
Run Code Online (Sandbox Code Playgroud)

因此,如果你使用任何整数蓝图(bigInteger,mediumInteger,tinyInteger,smallInteger,等...)与任何第二个参数(0以外),你是在告诉Laravel,从而与整数auto_increment性能,这将返回:

Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key")

  • 是的,你正确的.在file/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint中我们可以找到定义:`public function bigInteger($ column,$ autoIncrement = false,$ unsigned = false)`.所以20 ==真:) (15认同)
  • 一个解释会很好(不是对我来说,我知道为什么,但是对其他人,可能还有OP) (2认同)

hab*_*bib 6

通过以下方式指定 bigInteger 列的大小

$table->bigInteger('uid')->length(20)->unsigned();
Run Code Online (Sandbox Code Playgroud)

  • 这对我有帮助,我使用 ('uid', 20) 而不是长度方法。 (2认同)