Skip to content

fix: dump relation fields in declaration order - #2067

Open
itzzdev09 wants to merge 1 commit into
ormar-orm:masterfrom
itzzdev09:fix/relation-dump-order
Open

itzzdev09 wants to merge 1 commit into
ormar-orm:masterfrom
itzzdev09:fix/relation-dump-order

Conversation

@itzzdev09

Copy link
Copy Markdown
Contributor

Fixes #2063

_extract_nested_models iterated extract_related_names(), which returns a set, so the relation keys in model_dump() / model_dump_json() came out in string-hash order and changed between processes with PYTHONHASHSEED.

It now walks the relation names in ormar_config.model_fields order, so relations are dumped in declaration order. extract_related_names() itself is unchanged (it is used for set operations in many places).

Tests (tests/test_exclude_include_dict/test_dump_relation_order.py):

  • dump keys and JSON match declaration order
  • the same dump run in subprocesses with PYTHONHASHSEED 0, 2 and 42 gives one result

Both fail on master and pass with the change. Full suite on SQLite: the only failures are the 7 test_pool_reset MySQL cases that need asyncmy/aiomysql, same as on master.

_extract_nested_models walked the relation names returned by
extract_related_names(), which is a set, so the order of relation keys
in model_dump()/model_dump_json() followed string hash order and changed
with PYTHONHASHSEED. Walk them in model_fields order instead.

Fixes ormar-orm#2063
@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 Dec 6, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning, 1 note

⚠️ 122 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

ℹ️ 143 benchmarks have no flamegraph

The flamegraph wasn't generated: this Python version can't be profiled. Use CPython 3.12 or later on Linux x86-64 or ARM64 with glibc, e.g., from actions/setup-python or uv.

See affected benchmarks

✅ 143 untouched benchmarks


Comparing itzzdev09:fix/relation-dump-order (d30e986) with master (3fcfd93)

Open in CodSpeed

This branch has not been deployed

No deployments
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.

model_dump() order of relation fields depends on PYTHONHASHSEED

2 participants