Inherit Meta.ordering from abstract base models (#2046)#2236
Open
uttam12331 wants to merge 1 commit into
Open
Conversation
When a model defined its own ``Meta`` (e.g. to set ``table``) but did not set ``ordering``, the ordering declared on an abstract base model's ``Meta`` was lost, because ``ModelMeta.__new__`` reads ``Meta`` only from the class's own attributes. As a result queries on the subclass came back unordered. If the subclass's own ``Meta`` does not define ``ordering``, inherit the default ordering from the first base model that has one. A subclass that defines its own ``ordering`` (including an explicitly empty one) is unaffected. Closes tortoise#2046
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #2046.
When a model defines its own
Meta(for example to settable) but does not setordering, theorderingdeclared on an abstract base model'sMetawas silently dropped, so queries on the subclass came back unordered:Root cause
ModelMeta.__new__builds the model'sMetaInfofromattrs.get("Meta", ...)— i.e. only the class's ownMeta. When the subclass supplies its ownMetawithoutordering, the abstract base's ordering is never consulted.Fix
After building the meta, if the subclass's own
Metadoes not defineordering, inherit the default ordering from the first base model that has one. This mirrors how abstract-base Meta options behave in similar ORMs.Behavior is preserved for models that:
ordering(theirs wins), orordering = [](stays empty).Tests
Added
test_abstract_meta_ordering_is_inherited, covering inheritance, own-ordering-wins, and explicit-empty-stays-empty. It fails ondevelopand passes with this change. Existingtest_inheritance.py/test_order_by.pystill pass;ruff check/ruff formatare clean; added a CHANGELOG entry.