Shafi
Shafi

Reputation: 1970

How can I make auto-increment columns non-auto-increment?

I am trying to create a table with this code.

public function up()
{
    Schema::create('BookInfo', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('bookId',11);
        $table->string('Name',255);
        $table->string('Publisher')->nullable();
        $table->integer('Publishing_year',4)->nullable();
        $table->integer('Price',5)->nullable();
        $table->string('Language',30);
        $table->timestamps();
    });
}

When I tried php artisan migrate it shows me this error.

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key (SQL: create table
BookInfo(bookIdint not null auto_increment primary key,Namevarchar(255) not null, Publishervarchar(255) null,Publishing_yearint null auto_increment primary key,Priceint null auto_increment primary key, Languagevarchar(30) not null,created_attimestamp default 0 not null,updated_attimestamp default 0 not null) default character set utf8 collate utf8_unicode_ci) and

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key

It seems laravel takes all the integer columns as auto-increment.What happened here actually and what will be solution?

Upvotes: 1

Views: 450

Answers (2)

Mostafiz Rahman
Mostafiz Rahman

Reputation: 8522

$table->integer('bookId',11); is not syntactically correct in Eloquent ORM(you can't set any size limit for integers) and that is causing the error in your case.

And $table->increments('id'); automatically sets id as a primary key.

Find all necessary table building commands in Eloquent ORM here: http://laravel.com/docs/4.2/schema#adding-columns

Upvotes: 1

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

The error says it all,

$table->integer('bookId',11);
                          ^// this is considered as autoincrement rather than length

There is no length option for integers

See this

References on SO, this and this

Upvotes: 3

Related Questions