Php Laravel:相同的表关系错误

Php Laravel:相同的表关系错误,php,mysql,laravel-5,Php,Mysql,Laravel 5,我正在用JSTree开发一个目录层次结构 我正在设置数据库列以及与模式生成器的关系。 这是我的 Schema::create('directories', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('text'); $table->integer('parent

我正在用JSTree开发一个目录层次结构

我正在设置数据库列以及与模式生成器的关系。 这是我的

 Schema::create('directories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('text');
        $table->integer('parent_id')->default(0)->unsigned();
        $table->foreign('parent_id')->references('id')->on('directories')->onUpdate('cascade')->onDelete('cascade');
        $table->timestamps();

    });
在迁移过程中,我没有收到任何错误,但是当我尝试插入一条记录时,我得到的错误是

SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(“dms”。“目录”,约束“目录”\u父级\u id\u外键”(“父级\u id”)在更新级联时删除级联时引用“目录”(“id”)(SQL:插入到“目录”('name'、'text'、'parent_id'、'updated_at'、'created_at')值(项目,项目,012018-01-20 12:50:372018-01-20 12:50:37))


这可能有什么问题。谢谢您不能
外键设置为
0
,而是从
目录
中提供一个真正的id作为
父id
,或者您可以将您的模式更改为以下,以允许
作为
父id

Schema::create('directories', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('text');
    $table->integer('parent_id')->nullable()->unsigned();
    $table->foreign('parent_id')->references('id')->on('directories')->onUpdate('cascade')->onDelete('cascade');
    $table->timestamps();

});

在您的
insert
方法中,如果我使用
signed(),您可以跳过
parent\u id
或设置为
null

我可以使用零作为外键吗?不,符号值也指负值,但外键不能为负值,因此我建议将其保留为null,这样您可以无错误地进行查询,当您从该关系检索值时,保留零需要额外检查