Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

laravel

public function up()
{
    Schema::create('jadwal_praks', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('thnajrn_id', 10)->unsigned();
        $table->foreign('thnajrn_id')->references('id')->on('tahun_ajarans');
        $table->integer('prak_id', 10)->unsigned();
        $table->foreign('prak_id')->references('Prak_kode')->on('mata_praks');
        $table->integer('hari_id', 10)->unsigned();
        $table->foreign('hari_id')->references('id')->on('haris');
        $table->integer('jam_id', 10)->unsigned();
        $table->foreign('jam_id')->references('id')->on('jams');
        $table->integer('ruang_id', 10)->unsigned();
        $table->foreign('ruang_id')->references('id')->on('ruangs');
        $table->integer('kap_id', 10)->unsigned();
        $table->foreign('kap_id')->references('id')->on('kapasitas');

        $table->timestamps();
        $table->rememberToken();
    });
}

After run php artisan migrate

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto co lumn and it must be defined as a key (SQL: create table jadwal_praks (id int unsigned not null auto_increment
primary key, thnajrn_id int unsigned not null auto_increment primary key, prak_id int unsigned not null auto _increment primary key, hari_id int unsigned not null auto_increment primary key, jam_id int unsigned not nul l auto_increment primary key, ruang_id int unsigned not null auto_increment primary key, kap_id int unsigned
not null auto_increment primary key, created_at timestamp null, updated_at timestamp null, remember_token v archar(100) null) default character set utf8 collate utf8_unicode_ci)

And this

[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

like image 338
Muhammad Adnan Syarief Avatar asked Feb 05 '17 14:02

Muhammad Adnan Syarief


2 Answers

With my observation, I would say you should remove default value you added to the your foreign fields from (for example):

$table->integer('thnajrn_id', 10)->unsigned(); 

To:

$table->integer('thnajrn_id')->unsigned();

PS: With a similar experience, I know presently this works with one of the projects I work with in Laravel 5.2.*

Hope this helps :)

like image 75
Oluwatobi Samuel Omisakin Avatar answered Nov 15 '22 19:11

Oluwatobi Samuel Omisakin


When you use $table->integer('thnajrn_id', 10)->unsigned(); this means your setting the second parameter as true which represents AutoIncrement

try

table->integer('thnajrn_id')->length(10)->unsigned();

More info

like image 22
benson Njung'e Avatar answered Nov 15 '22 18:11

benson Njung'e