简体   繁体   中英

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

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

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

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

When you use $table->unsignedBigInteger('account_id')->nullable(); you can still get this same error, since nullable() produces a default value can produce that same error. So always leave your foreign key empty, neither nullable or default(0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM