Laravel 表:只能有一个 auto 列,并且必须将其定义为键

2022-08-30 19:40:48

我已经使所有整数无符号,但我仍然得到错误。我需要更改哪些内容?

<?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');
    }

}

SQLSTATE[42000]:语法错误或访问违规:1075表定义不正确;只能有一个 auto 列,并且必须将其作为键进行 de 罚款

谢谢。


答案 1

$table->bigInteger('uid')->unsigned();

而不是

$table->bigInteger('uid', 20)->unsigned();

信息:

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

public function bigInteger($column, $autoIncrement = false, $unsigned = false)

所以 20(不是 0)评估为 。true

虽然方法具有作为第二个参数:stringlength

public function string($column, $length = null)

因此,如果您将任何整数蓝图(,,,,等)与任何第二个参数(0除外)一起使用,您将告诉Laravel使用属性创建一个整数,这将返回:bigIntegermediumIntegertinyIntegersmallIntegerauto_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")


答案 2

按以下方式指定 bigInteger 列的大小

$table->bigInteger('uid')->length(20)->unsigned();

推荐