Opened 15 years ago

Closed 15 years ago

Last modified 13 years ago

#11606 closed (duplicate)

return error data when using only() and select_related() at the same times that models foreign key is set to default null.

Reported by: sean.dong Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: only, select_related, defer
Cc: buburno+django@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

>>> from station.models import Station_Building
>>> s1=Station_Building.objects.select_related('building').only('azimuth','building__level').filter(building__isnull=False)
>>> s1[0].building.level
0 #wrong value
>>> s1=Station_Building.objects.select_related('building').filter(building__isnull=False)
>>> s1[0].building.level
1 #right value
>>> 

Change History (6)

comment:1 by Alex Gaynor, 15 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Bruno Renié, 15 years ago

Cc: buburno+django@… added

I have tried to look at this without success. It seems that the generated SQL is perfectly fine but some columns get mixed when building the python representation.

I have some models that looks like this:

class Category(models.Model):
    color = models.CharField(max_length=50)

class Feed(models.Model):
    name = models.CharField(max_length=255)
    category = models.ForeignKey(Category, related_name='feeds')

class Entry(models.Model):
    feed = models.ForeignKey(Feed, related_name='entries')
    title = models.CharField(max_length=255)
    date = models.DateTimeField()
    read = models.BooleanField(default=False, db_index=True)
    user = models.ForeignKey(User, related_name='entries')

If I do:

entries = Entry.objects.select_related().only('id', 'feed', 'title', 'date',
                                              'read', 'feed__name',
                                              'feed__category__color')

then:

for e in entries:
    print e.feed.category.color # prints datetime.datetime instances (all the same) - related user's creation date
    print e.feed.category.id # prints the related user's password hash sha1$cf481$01b13cdfab3.....

So, the problem may not be specific to foreign keys, and the engine fetches information from wrong related tables.
This happens at least on PostgreSQL, I can't confirm on other backends.

comment:3 by Russell Keith-Magee, 15 years ago

#11844 seems to be a duplicate, with another test case.

comment:4 by Russell Keith-Magee, 15 years ago

milestone: 1.2

comment:5 by Russell Keith-Magee, 15 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #12851, fixed in r12817.

comment:6 by Jacob, 13 years ago

milestone: 1.2

Milestone 1.2 deleted

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