Repository navigation
feat: read-only models for database views (relation keys, phase 6) - #1932
Conversation
…ro keys, release notes)
…se-2 # Conflicts: # ormar/models/descriptors/descriptors.py # tests/test_model_definition/test_primary_key.py
…N, persisted state)
…rgets, serialization)
…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.
…o feat/relation-keys-phase-4a
Detached copies keep forward relations since phase 3a, so the opt-outs are no longer needed.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Merging this PR will improve performance by 58.45%1 warning, 1 note
Performance Changes
Tip Curious why performance improved? Comment Comparing |
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 newReadOnlyModelError, and its table is kept out ofmetadata.create_all()/ Alembic. Foreign keys from a view never create constraints, foreign keys to a view needdb_constraint=False, and proxies, abstract-parent children and many-to-many relations of views behave accordingly. Docs: newdocs/models/views.md, views section indocs/models/migrations.md.