Skip to content

fix: insert child with preset pk in reverse relation add() - #1822

Merged
collerek merged 4 commits into
ormar-orm:masterfrom
moneebullah25:fix/relation-add-insert-child-with-pk
Oct 12, 2026
Merged

collerek merged 4 commits into
ormar-orm:masterfrom
moneebullah25:fix/relation-add-insert-child-with-pk

Conversation

@moneebullah25

Copy link
Copy Markdown
Contributor

Fixes #1819

Problem

owner.children.add(child) on a reverse FK called child.upsert(). When the child already had a pk set (a UUID default, or an explicit id with autoincrement=False), upsert() took the update path and no row was inserted, so the add silently did nothing.

Change

RelationProxy.add() now calls item.upsert(__force_save__=True), the same flag already used in save_mixin.py and model.py. A child with a preset pk is inserted, and a child that already exists in the database is still updated.

Tests

Added to tests/test_queries/test_adding_related.py:

  • child with a default UUID pk is inserted
  • child with an explicit non-autoincrement pk is inserted
  • an existing (already saved) child is updated
  • a fresh instance with an existing pk updates that row

What was run

  • ruff format --check and ruff check on both changed files: pass
  • mypy ormar tests/test_queries/test_adding_related.py --ignore-missing-imports: pass
  • pytest tests/test_queries/test_adding_related.py (sqlite): 5 passed
  • Full suite and the postgres/mysql backends were not run.

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 114 untouched benchmarks
🆕 9 new benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
🆕 test_adding_new_children_to_reverse_relation[10] N/A 84.5 ms N/A
🆕 test_adding_new_children_to_reverse_relation[20] N/A 168.4 ms N/A
🆕 test_adding_new_children_to_reverse_relation[40] N/A 337.3 ms N/A
🆕 test_adding_saved_children_to_reverse_relation[10] N/A 110.1 ms N/A
🆕 test_adding_saved_children_to_reverse_relation[20] N/A 221.1 ms N/A
🆕 test_adding_saved_children_to_reverse_relation[40] N/A 440.4 ms N/A
🆕 test_saving_related_new_children[10] N/A 91.4 ms N/A
🆕 test_saving_related_new_children[20] N/A 174.7 ms N/A
🆕 test_saving_related_new_children[40] N/A 340.9 ms N/A

Comparing moneebullah25:fix/relation-add-insert-child-with-pk (a9a2967) with master (310df0c)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (ee66c82) during the generation of this report, so 310df0c was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@collerek
collerek merged commit 94bee42 into ormar-orm:master Oct 12, 2026
11 checks passed
@collerek

Copy link
Copy Markdown
Collaborator

Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RelationProxy.add() on a reverse FK silently does not insert a child that has a pk set (UUID default or explicit id)

3 participants