Opened 16 years ago

Closed 16 years ago

Last modified 13 years ago

#10710 closed (fixed)

Invalid results with only() when model has two foreign keys to other models that inherit from a single abstract parent

Reported by: mrts Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: defer only, efficient-admin
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Given the models:

from django.db import models

class Base(models.Model):
    name = models.CharField(max_length=10)
    lots_of_text = models.TextField()

    class Meta:
        abstract = True

    def __unicode__(self):
        return self.name

class A(Base):
    a_field = models.CharField(max_length=10)

class B(Base):
    b_field = models.CharField(max_length=10)

class C(Base):
    a = models.ForeignKey(A)
    b = models.ForeignKey(B)
    is_published = models.BooleanField()

the following occurs:

# all ok here:
>>> from somewhere.models import A, B, C
>>> results = C.objects.all().select_related()
>>> results[0].a.id
2
>>> results[0].a.lots_of_text
u'Sed ut perspiciatis unde omnis iste natus error...'

# bizarre things happen: `id` and `lots_of_text` are swapped
>>> results = C.objects.all().only('name', 'a', 'b').select_related()
>>> results[0].a.lots_of_text
2
>>> results[0].a.id
u'Sed ut perspiciatis unde omnis iste natus error...'

# no luck in deferring two relations with only():
>>> C.objects.all().only('name', 'a', 'b').select_related().only('a__name', 'b__name')
Traceback (most recent call last):
...
FieldDoesNotExist: A has no field named 'b'

# deferring a single relation with only works:
>>> C.objects.all().only('name', 'a', 'b').select_related().only('a__name')
[<C_Deferred_b_is_published_lots_of_text_name: c1>, ...]

Change History (7)

comment:1 by mrts, 16 years ago

Summary: limit() behaves erratically in a queryset where the model has two foreign keysonly() behaves erratically in a queryset where the model has two foreign keys

comment:2 by mrts, 16 years ago

Summary: only() behaves erratically in a queryset where the model has two foreign keysinvalid results with only() when model has two foreign keys to other models that inherit from a single abstract parent

comment:3 by mrts, 16 years ago

Summary: invalid results with only() when model has two foreign keys to other models that inherit from a single abstract parentInvalid results with only() when model has two foreign keys to other models that inherit from a single abstract parent

comment:4 by Malcolm Tredinnick, 16 years ago

(In [10383]) Fixed deferred fields and select_related() interaction.

Loading related models when some fields were deferred was resulting in
incorrect offsets being used into the results row, causing the wrong data to be
assigned to attributes.

Refs #10710. This fixes the first of two bugs reported there.

comment:5 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: newclosed

The second problem here was fixed in r10384.

comment:6 by mrts, 16 years ago

Keywords: efficient-admin added

comment:7 by Jacob, 13 years ago

milestone: 1.1

Milestone 1.1 deleted

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