Skip to content

feat: read-only models for database views (relation keys, phase 6) - #1932

Merged
collerek merged 45 commits into
masterfrom
feat/relation-keys-phase-6-views
Nov 5, 2026
Merged

collerek merged 45 commits into
masterfrom
feat/relation-keys-phase-6-views

Conversation

@collerek

@collerek collerek commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator

Closes #970. OrmarConfig(view=True) maps a model to an existing database view: it can be queried, joined and prefetched like any model, writes (model, queryset and relation writes) raise the new ReadOnlyModelError, and its table is kept out of metadata.create_all() / Alembic. Foreign keys from a view never create constraints, foreign keys to a view need db_constraint=False, and proxies, abstract-parent children and many-to-many relations of views behave accordingly. Docs: new docs/models/views.md, views section in docs/models/migrations.md.

…se-2

# Conflicts:
#	ormar/models/descriptors/descriptors.py
#	tests/test_model_definition/test_primary_key.py
…phase-2

# Conflicts:
#	docs/releases.md
#	ormar/fields/foreign_key.py
#	ormar/fields/many_to_many.py
#	ormar/models/descriptors/descriptors.py
#	ormar/models/helpers/sqlalchemy.py
#	ormar/models/mixins/save_mixin.py
#	ormar/models/model.py
#	ormar/models/newbasemodel.py
#	ormar/models/primary_key.py
#	ormar/queryset/clause.py
#	ormar/queryset/queryset.py
#	ormar/queryset/templates/writes.py
#	tests/test_model_definition/test_primary_key.py
Model.pk on a composite key is now a key accessor: == and in_() / <<
compile to the same row-value comparison as pk= / pk__in=, and asc() /
desc() order by every key field. .pk also works after relations
(Book.author.pk == 1) for single and composite keys.
Ordering by pk only worked in order_by() on the queried model. pk after
relations (order_by("stock__pk"), Shop.stock.pk.desc()), ormar_config
order_by=["-pk"] and relation orders_by / related_orders_by produced
invalid SQL, for single keys too. A shared order_actions_for() helper now
expands pk into the key fields of the model the chain points at, and is
used by QuerySet.order_by, model default ordering and join orderings.
…phase-3a

# Conflicts:
#	docs/models/primary-keys.md
#	docs/releases.md
#	ormar/fields/foreign_key.py
#	ormar/models/model.py
#	ormar/models/newbasemodel.py
#	ormar/models/primary_key.py
#	ormar/queryset/clause.py
#	tests/test_model_definition/test_composite_pk_definition.py
Detached copies dropped their relations, so a required foreign key (one in
the primary key) read None and the validated rebuild failed. Register bare
copies of the forward relations instead and run the reverse select_related
test with the cross-check on.
Detached copies keep forward relations since phase 3a, so the opt-outs
are no longer needed.
@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 Nov 5, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 58.45%

1 warning, 1 note

⚠️ 112 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

ℹ️ 126 benchmarks have no flamegraph

The flamegraph wasn't generated: the symbols of the profiling trace could not be resolved, because no information about the language frames was collected.

See affected benchmarks

⚡ 2 improved benchmarks
✅ 124 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_get_documents_page_with_json[250] 35.3 ms 15.5 ms ×2.3
⚡ test_get_one_wide_model[250] 11.1 ms 10 ms +10.32%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing feat/relation-keys-phase-6-views (f134b82) with master (dc308af)

Open in CodSpeed

@collerek
collerek merged commit 607e3f2 into master Nov 5, 2026
12 checks passed
@collerek
collerek deleted the feat/relation-keys-phase-6-views branch November 5, 2026 09:23
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.

Querying DB Views using Ormar

2 participants