Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have made all integers unsigned but I still get the error. What do I need to change?

<?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]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be de fined as a key

Thank you.

like image 398
Smiley Avatar asked Mar 05 '15 18:03

Smiley


2 Answers

Use

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

instead of

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

Infos:

in /vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint you can see the bigInteger function:

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

So 20 (not being 0) evals to true.

While string method has as second parameter length:

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

Therefore if you use any integer blueprint (bigInteger, mediumInteger, tinyInteger, smallInteger, etc...) with any second parameter (other than 0) you are telling Laravel to make an integer with an auto_increment property, this will return:

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

like image 191
newatol Avatar answered Sep 27 '22 15:09

newatol


Specify the size for bigInteger column by following way

$table->bigInteger('uid')->length(20)->unsigned();
like image 33
habib Avatar answered Sep 27 '22 15:09

habib