Tuesday, March 28, 2017

[Laravel 5][Resolved] syntax error or access violation: key column doesn't exists in table



Error message :
[PDOException]
SQLSTATE[42000] : Syntax error or access violation: 1072 key column 'key_id' doesn't exists in table
The error caused by you missed creating the column "key_id" before you assign it as foreign key.

There is the example with problem:
Schema::create('tables', function (Blueprint $table) {
    $table->increments('d')->uniqlue();
    $table->foreign('key_id',10)->references('news_id')->on('news')->nullable();
    $table->string('title',30)->nullable();
    $table->timestamps();
    $table->softDeletes();
});
Corrected code:
Schema::create('tables', function (Blueprint $table) {
    $table->increments('d')->uniqlue();
    $table->integer('key_id')->unsigned()->nullable();
    $table->foreign('key_id',10)->references('news_id')->on('news')->nullable();
    $table->string('title',30)->nullable();
    $table->timestamps();
    $table->softDeletes();
});

Reference

https://stackoverflow.com/questions/33633684/laravel-migration-cant-add-foreign-key

No comments :

Post a Comment