【问题标题】:Syntax error or access violation: 1072 Key column - self-join语法错误或访问冲突:1072 键列 - 自加入
【发布时间】:2017-01-30 08:44:21
【问题描述】:

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

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

我想要什么:

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();
    });
}

【问题讨论】:

    标签: laravel laravel-5


    【解决方案1】:

    试试这个,

       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();
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      • 2015-03-23
      • 1970-01-01
      • 2013-06-01
      • 2016-10-19
      • 2019-02-10
      相关资源
      最近更新 更多