Opened 7 years ago

Closed 7 years ago

#11199 closed (invalid)

Select_related added to the query set will produce empty result

Reported by: milosu Owned by: nobody
Component: Database layer (models, ORM) Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by Alex Gaynor)

You have a model like:

class Driver:
    name = models.CharField(max_length=100)

class Car:
    name = models.CharField(max_length=100)
    driver = models.ForeignKey(Driver)

Inside the database mangle the fkey from car to driver as nullable and insert some records into car table without the reference to the driver table.

Than just try:

Car.objects.filter(name = 'something').select_related()

and you will get empty queryset when evaluated.

I guess this is a correct ORM behaviour when using inner joins etc.

Could this be documented somewhere? It was quite mysterious for me to make an observation like this and to find the problem (missing null=True model declaration)..

Change History (2)

comment:1 Changed 7 years ago by Alex Gaynor

Description: modified (diff)
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

Please use preview.

comment:2 Changed 7 years ago by Alex Gaynor

Resolution: invalid
Status: newclosed

Marking as invalid, your Models need to match your DB.

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