Titu
Titu

Reputation: 61

Laravel database migration Incorrect table definition

public function up()
    {
        Schema::create('materials', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name',60);
            $table->integer('category_id',10);
            $table->integer('low_bulk',10);
            $table->integer('high_bulk',10);
            $table->integer('order_level',10);
            $table->decimal('unit_price',10,2);
            $table->string('description');
            $table->timestamps();
        });
    }

This schema gives me this following error

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

Can anyone tell me what should I change?

Upvotes: 0

Views: 75

Answers (2)

Igor Carvalho
Igor Carvalho

Reputation: 698

Try again like this.

 public function up()
        {
            Schema::create('materials', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name',60);
                $table->integer('category_id);
                $table->integer('low_bulk');
                $table->integer('high_bulk');
                $table->integer('order_level');
                $table->decimal('unit_price',10,2);
                $table->string('description');
                $table->timestamps();
            });
        }

For more details, you can refer this.

Upvotes: 1

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25906

The second parameter of integer() is not the length but whether the column should be auto-incrementing. PHP converts 10 to true and tries to make all these columns auto-incrementing.

Just remove it, the length doesn't have any effect on MySQL:

Schema::create('materials', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name',60);
    $table->integer('category_id');
    $table->integer('low_bulk');
    $table->integer('high_bulk');
    $table->integer('order_level');
    $table->decimal('unit_price',10,2);
    $table->string('description');
    $table->timestamps();
});

Upvotes: 2

Related Questions