繁体   English   中英

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

[英]Laravel table: there can be only one auto column and it must be defined as a key

我已将所有整数设为无符号,但仍然出现错误。 我需要改变什么?

<?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 表定义不正确; 只能有一个自动列,并且必须将其定义为键

谢谢你。

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

代替

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

信息:

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

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

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

虽然string方法具有作为第二个参数length

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

因此,如果您使用任何带有任何第二个参数(0 除外)的整数蓝图( bigIntegermediumIntegertinyIntegersmallInteger等...),您是在告诉 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")

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM