简体   繁体   English

Laravel-违反完整性约束:1452无法添加或更新子行

[英]Laravel - Integrity constraint violation: 1452 Cannot add or update a child row

I am importing some content from remote WP database to local DB with laravel. 我正在使用laravel将一些内容从远程WP数据库导入本地数据库。 I have set up the tables inventories and contents. 我已经设置了表的清单和内容。

Inventories table looks like this: 库存表如下所示:

    Schema::create('inventories', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('remote_id')->unsigned();
        $table->integer('local_id')->unsigned();
        $table->string('local_type');
        $table->timestamps();

        $table->foreign('local_id')->references('id')->on('contents')->onDelete('cascade');
    });

This is the contents table: 这是内容表:

    Schema::create('contents', function (Blueprint $table) {
        $table->increments('id')->unsigned();
        $table->integer('ct_id')->unsigned();
        $table->foreign('ct_id')->references('id')->on('content_types');
        $table->integer('cms_id');
        $table->string('title');
        $table->string('slug');
        $table->text('excerpt');
        $table->mediumText('body');
        $table->string('password');
        $table->integer('parent_id');
        $table->timestamps();
    });

I have made functions to import all and import single posts from remote WP DB. 我已经实现了从远程WP DB导入所有内容和导入单个帖子的功能。 This is the import for all posts: 这是所有帖子的导入:

public function all()
{
    $include      = array_diff($this->indexable, ['folder']);
    $publishedPostsIDs = $this->import($include);
    $this->deleteOldContent($publishedPostsIDs);
}

private function import($include)
{
    $posts             = Post::where('post_status', 'publish')->whereIn('post_type', $include)->get();
    foreach ($posts as $post) {
        $publishedPostsIDs[] = $post->ID;
        $this->contentInterface->updateOrCreateInventory($post->ID);
    }

    return $publishedPostsIDs;
}

public function updateOrCreateInventory($remoteId)
{
    $this->contentInterface->updateOrCreateInventory($remoteId);
}

private function deleteOldContent($publishedPostsIDs)
{
    $contentToDelete = Content::whereNotIn('cms_id', $publishedPostsIDs)->get();

    if (count($contentToDelete) > 0) {
        foreach ($contentToDelete as $content) {
            $content->delete();
        }
    }
}

So, when I am importing all, I just go to the route that goes to all function, and all the posts from remote WP DB that are not of post type folder are imported. 因此,当我导入所有内容时,我只是转到all功能的路径,并且所有来自远程WP DB的非帖子类型文件夹的帖子都被导入了。 If I want to import a single post from remote DB, then I have a route that goes directly to the updateOrCreateInventory function. 如果我想从远程数据库导入单个帖子,那么我有一条直接转到updateOrCreateInventory函数的路由。 I also have import for the posts of type folder, which is basically almost the same as the function all . 我还导入了类型文件夹的帖子,基本上与all函数几乎相同。

public function folder()
{
    $include = ['folder'];
    $importResult = $this->import($include);

    return $importResult['imported'];
}

The problem I have is that when I am importing all folders at once I get an error: 我的问题是,当我一次导入所有文件夹时,出现错误:

QueryException in Connection.php line 729: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ( middleton . inventories , CONSTRAINT inventories_local_id_foreign FOREIGN KEY ( local_id ) REFERENCES contents ( id ) ON DELETE CASCADE) (SQL: insert into inventories ( remote_id , updated_at , created_at ) values (7512, 2017-11-13 15:33:17, 2017-11-13 15:33:17)) QueryException在Connection.php行729:SQLSTATE [23000]:完整性约束违规:1452不能添加或更新子行,外键约束失败( middletoninventories ,约束inventories_local_id_foreign外键( local_id )参考contentsid )ON DELETE CASCADE)(SQL:插入inventoriesremote_idupdated_atcreated_at )值(7512,2017-11-13 15:33:17,2017-11-13 15:33:17))

But, if I try to import that same folder individually , or to be more exact that same post of type folder, I don't get any error and the post is imported. 但是,如果我尝试分别导入同一文件夹,或者更确切地说是同一文件夹类型的帖子,则不会出现任何错误,并且该帖子也已导入。 How is that possible? 那怎么可能?

You $this->contentInterface creates an Inventory model without the "local_id" field specified, but it is required by the foreign key. $ this-> contentInterface会创建一个未指定“ local_id”字段的清单模型,但是外键是必需的。

Find the place where you create the Inventory model and provide a valid "local_id". 查找创建库存模型的位置并提供有效的“ local_id”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 违反完整性约束 - 1452 无法添加或更新子行 - Integrity constraint violation - 1452 Cannot add or update a child row 违反完整性约束:1452 无法添加或更新子行: - Integrity constraint violation: 1452 Cannot add or update a child row: SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败-Laravel - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - laravel Laravel-SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败 - Laravel - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel Seed:违反完整性约束:1452无法添加或更新子行:外键约束失败 - Laravel Seed: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel 6:完整性约束违规:1452 无法添加或更新子行:外键约束失败 - Laravel 6: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel 6 - SQLSTATE[23000]:违反完整性约束:1452 无法添加或更新子行:外键约束失败 - Laravel 6 - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel 5:违反完整性约束:1452 无法添加或更新子行:外键约束失败 - Laravel 5: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel 5.2 - 违反完整性约束:1452 无法添加或更新子行:外键约束失败 - Laravel 5.2 - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel:SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败 - Laravel: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM