【问题标题】:Laravel table: there can be only one auto column and it must be defined as a keyLaravel 表:只能有一个自动列,并且必须定义为键
【发布时间】:2015-05-07 05:44:50
【问题描述】:

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

<?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 列,并且必须定义为 key

谢谢。

【问题讨论】:

    标签: php mysql laravel


    【解决方案1】:

    使用

    $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)
    

    因此,如果您将任何整数蓝图(bigIntegermediumIntegertinyIntegersmallInteger 等...)与任何第二个参数(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")

    【讨论】:

    • 一个解释会很好(不适合我,我知道为什么,但对于其他人,可能还有 OP)
    • 是的,你说得对。在文件 /vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint 我们可以找到定义:public function bigInteger($column, $autoIncrement = false, $unsigned = false) 。所以 20 == true :)
    【解决方案2】:

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

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

    【讨论】:

    • 这对我有帮助,我使用的是 ('uid', 20) 而不是长度方法。
    猜你喜欢
    • 1970-01-01
    • 2020-01-01
    • 2021-07-21
    • 2019-05-23
    • 2015-04-05
    • 2013-07-12
    • 1970-01-01
    • 2017-05-31
    • 2013-12-17
    相关资源
    最近更新 更多