【问题标题】:LARAVEL Migrate ERROR SQLSTATE[42000] "exist in table"LARAVEL 迁移错误 SQLSTATE[42000]“存在于表中”
【发布时间】:2017-09-13 16:18:06
【问题描述】:

运行 artisan migrate 时出现错误

首先我想为多对多关系运行并向多对多关系添加一个表,当我迁移时发生的情况是终端上出现错误。

    [Illuminate\Database\QueryException]                                         
  SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'hobi' d  
  oesn't exist in table (SQL: alter table `hobi_siswa` add constraint `hobi_s  
  iswa_hobi_foreign` foreign key (`hobi`) references `hobi` (`id`) on delete   
  cascade on update cascade)                                                   



  [PDOException]                                                               
  SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'hobi' d  
  oesn't exist in table             

据说是终端,但我很惊讶和困惑,为什么它的目的同样是关键关系。

  <?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableHobiSiswa extends Migration
{
    public function up()
    {
        //create table hobi siswa
        Schema::create('hobi_siswa', function (Blueprint $table) {
            $table->integer('id_siswa')->unsigned()->index();
            $table->integer('id_hobi')->unsigned()->index();
            $table->timestamps();

            //set PK
            $table->primary(['id_siswa', 'id_hobi']);

            //set FK hobi siswa --- siswa
            $table->foreign('id_siswa')
                  ->references('id')
                  ->on('hobi')
                  ->onDelete('cascade')
                  ->onUpdate('cascade');

            //Set FK Hobi_siswa ---hobi
            $table->foreign('hobi')
                  ->references('id')
                  ->on('hobi')
                  ->onDelete('cascade')
                  ->onUpdate('cascade');
        });

    }

    public function down()
    {
        Schema::drop('hobi_siswa');
    }
}

它是 createTableHobiSiswa 我做我想做的迁移

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableHobi extends Migration
{

    public function up()
    {
        Schema::create('hobi', function (Blueprint $table) {
            $table->increments('id');
            $table->string('nama_hobi');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('hobi');
    }
}

这与 createTableHobi 相关

【问题讨论】:

    标签: mysql database laravel migration migrate


    【解决方案1】:

    在我看来,您在第二个外键链中给 foreign() 的值引用了列 hobi,而表上的列实际上是 id_hobi

    尝试更改此部分:

    //Set FK Hobi_siswa ---hobi
    $table->foreign('hobi')
          ->references('id')
          ->on('hobi')
          ->onDelete('cascade')
          ->onUpdate('cascade');
    

    改为:

    //Set FK Hobi_siswa ---hobi
    $table->foreign('id_hobi') //reference the column on this table correctly
          ->references('id')
          ->on('hobi')
          ->onDelete('cascade')
          ->onUpdate('cascade');
    

    【讨论】:

      猜你喜欢
      • 2015-07-15
      • 1970-01-01
      • 2019-12-19
      • 2018-10-15
      • 2018-02-06
      • 2013-02-02
      • 2023-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多