Opened 17 years ago

Closed 16 years ago

#7136 closed (fixed)

when inheriting models, accessing related subclass instances can produce spurious relations depending on order

Reported by: Andy MacKinlay Owned by: socrates
Component: Core (Other) Version: dev
Severity: Keywords: model inheritance
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If you are using the new multi-table inheritance feature, and have two or more subclasses (eg SubClassA and SubClassB) which inherits from a baseclass, and retrieve a base class instance baseinstance from the DB, the expected behaviour if baseinstance only has a corresponding SubClassA instance should be to throw a DoesNotExist exception when accessing baseinstance.subclassb. However, if you have already accessed baseinstance.subclassa, baseinstance.subclassb will point to the corresponding SubClassA instance instead of raising the exception.

The attached models.py includes a test case that documents this.

Attachments (5)

models.py (1.4 KB ) - added by Andy MacKinlay 17 years ago.
model file with included doctest to replicate the issue
shell_results.py (4.6 KB ) - added by socrates 17 years ago.
This file is to easily reproduce the shell results and see the situation before and after the patch
patch_for_rev7513.diff (4.5 KB ) - added by socrates 17 years ago.
stricter type checking
patch_for_rev7513_alternative.diff (4.0 KB ) - added by socrates 17 years ago.
an alternative pach. in stead of stricter type checking it avoids name collisions by changing the cache_name.
shell_results_rev7513.py (5.4 KB ) - added by socrates 17 years ago.
This file is to easily reproduce the shell results and see the situation before and after the patch (this version includes the models)

Download all attachments as: .zip

Change History (12)

by Andy MacKinlay, 17 years ago

Attachment: models.py added

model file with included doctest to replicate the issue

comment:1 by socrates, 17 years ago

Owner: changed from nobody to socrates

comment:2 by socrates, 17 years ago

Status: newassigned

by socrates, 17 years ago

Attachment: shell_results.py added

This file is to easily reproduce the shell results and see the situation before and after the patch

comment:3 by socrates, 17 years ago

Has patch: set

As it tuns out the bug was even a bit more extensive: not only accessing a non existing subclass goes wrong. Also if the subclass does exist then in some special cases (multiple inheritance) the wrong object is returned (see shell_result.py). The patch (patch_for_rev7513.diff) contains a doctest that describes multiple model inheritance.

comment:4 by socrates, 17 years ago

The patch fixes the problem but I am not certain if this is the right way to go about it. The problem was initially caused because of name collisions in the SingleRelatedObjectDescriptor.cache_name. Maybe a different naming contvention for the cache_name is the right way to fix it.

by socrates, 17 years ago

Attachment: patch_for_rev7513.diff added

stricter type checking

by socrates, 17 years ago

an alternative pach. in stead of stricter type checking it avoids name collisions by changing the cache_name.

by socrates, 17 years ago

Attachment: shell_results_rev7513.py added

This file is to easily reproduce the shell results and see the situation before and after the patch (this version includes the models)

comment:5 by Alex Gaynor, 17 years ago

Triage Stage: UnreviewedAccepted

I'm marking this as accepted, here's another test I put together that shows the problem: http://dpaste.com/hold/48236/

comment:6 by socrates, 16 years ago

It seems that this bug has already been fixed somewhere else so this ticket can be closed as duplicate.

comment:7 by Alex Gaynor, 16 years ago

Resolution: fixed
Status: assignedclosed

This was indeed fixed(don't have the revision handy).

Note: See TracTickets for help on using tickets.
Back to Top