Skip to content

fix: raise QueryDefinitionError for related-model filters in update() and delete() - #1824

Merged
collerek merged 6 commits into
ormar-orm:masterfrom
moneebullah25:fix/update-delete-related-filter-1817
Oct 12, 2026
Merged

collerek merged 6 commits into
ormar-orm:masterfrom
moneebullah25:fix/update-delete-related-filter-1817

Conversation

@moneebullah25

Copy link
Copy Markdown
Contributor

Fixes #1817

Summary

QuerySet.update() and QuerySet.delete() build a statement against the model's own table only. When the queryset was filtered or excluded by a related model's field (for example filter(category__name="x")), the join was silently dropped, so the statement either ignored the condition or failed with an invalid SQL statement.

These methods now raise QueryDefinitionError naming the offending filter, via a small private helper. Filters on the model's own fields are unaffected.

Changes

  • ormar/queryset/queryset.py: guard in update() and delete().
  • tests/test_queries/test_queryset_level_methods.py: tests for filter and exclude on related fields in both methods.
  • docs/releases.md: release note under Fixes.

Checks run

  • ruff format --check and ruff check (ruff 0.16.10, matching the Makefile and poetry.lock) on the changed files: pass
  • mypy ormar tests/test_queries/test_queryset_level_methods.py --ignore-missing-imports: 4 errors, all pre-existing in untouched ormar/relations/*, none in changed files
  • pytest tests/test_queries/test_queryset_level_methods.py: 18 passed
  • Tests added for the new behaviour
  • Release note added
  • Full test suite across all database backends not run locally (only the touched test module, on SQLite)

@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 12, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 25.75%

ℹ️ 123 benchmarks have no flame graph

The flamegraph could not be generated because we failed to resolve the symbols in the profiling trace. This happens when we fail to harvest information about language frames.

❌ 2 (👁 2) regressed benchmarks
✅ 121 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
👁 ℹ️ test_creating_models_individually[10] 58.1 ms 79.1 ms -26.65%
👁 ℹ️ test_saving_wide_models_individually[20] 155.6 ms 207 ms -24.83%

Comparing moneebullah25:fix/update-delete-related-filter-1817 (89a2e76) with master (58f8494)

Open in CodSpeed

@collerek
collerek merged commit 8993d4e into ormar-orm:master Oct 12, 2026
11 checks passed
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.

update() and delete() with a related-model filter ignore the join

3 participants