Skip to content

Fix importer to update existing nodes when id provided (upsert behavior)#45

Merged
TimurKady merged 1 commit intomainfrom
fix-import-errors-for-existing-ids
Mar 3, 2026
Merged

Fix importer to update existing nodes when id provided (upsert behavior)#45
TimurKady merged 1 commit intomainfrom
fix-import-errors-for-existing-ids

Conversation

@TimurKady
Copy link
Copy Markdown
Owner

Motivation

  • The importer attempted to always create objects even when an id was present in the import payload, causing validation errors like {'id': ['Segment (Who) with this ID already exists.']}.
  • The goal is to perform upsert semantics per-level during import so existing rows are updated and new rows are created.

Description

  • Changed TreeNodeImporter.import_tree to collect ids for each depth level and load existing objects with in_bulk to decide between update vs create.
  • For rows matching existing objects the importer applies incoming fields (excluding internal tree fields _depth and _path and id) to the model instance, validates with full_clean() and calls save() to persist updates.
  • New objects are still constructed, validated and written with bulk_create, and counters created and updated are maintained in the import result.
  • Added a regression test TreeNodeImporterTests.test_importer_updates_existing_object_when_id_present to ensure importer updates an object when id is provided instead of attempting to re-create it.

Testing

  • Ran the full test suite with python -m pytest -q, which completed successfully with 15 passed, 1 skipped (and 3 warnings).
  • The added regression test verifies the upsert scenario and passes as part of the suite.

Codex Task

@TimurKady TimurKady added bug Something isn't working and removed codex labels Mar 3, 2026
@TimurKady TimurKady merged commit 3be8e22 into main Mar 3, 2026
5 checks passed
@TimurKady TimurKady deleted the fix-import-errors-for-existing-ids branch March 3, 2026 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant