语法错误或访问冲突:1072键列 - 自连接

时间:2016-09-21 22:55:43

标签: laravel laravel-5

我想为我的外键设置一个自定义名称。我能怎么做 ? 我使用下面的代码,但它让我错误:

  

SQLSTATE [42000]:语法错误或访问冲突:1072键列   ' parent_section_id'表中不存在(SQL:alter table   sections添加约束sections_parent_section_id _foreign   外键(parent_section_id)引用sectionsid)   删除级联)

我想要的是什么:

id - title - parent_section_id

parent_section_id引用当前的id表

public function up()
{
    Schema::create('sections', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->foreign('parent_section_id')->references('id')->on('sections')->onDelete('cascade');
        $table->timestamps();
    });
}

1 个答案:

答案 0 :(得分:2)

试试这个,

   public function up()
{
    Schema::create('sections', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('parent_section_id')->unsigned();

        $table->string('title');
        $table->foreign('parent_section_id')
              ->references('id')->on('sections')->onDelete('cascade');
        $table->timestamps();
    });
}