no objects found

2 views
Skip to first unread message

Brian

unread,
Jan 14, 2009, 7:54:30 PM1/14/09
to Django users
Django 1.0.2-1

If I do:

>>> object=models.software.objects.get(id=267)
>>> print models.software_installation.objects.filter(software=object)
[]
>>> print models.software_installation.objects.filter(software=object).count()
10

Huh? First statement says there are no results, next one says there
are 10.



If I spy on the packets, I see this in the SQL for the first
statement:

... LEFT OUTER JOIN `inventory_license_key` ON
(`inventory_software_installation`.`license_key_id` =
`inventory_license_key`.`id`) INNER JOIN `inventory_license` ON
(`inventory_license_key`.`license_id` = `inventory_license`.`id`) ...

If I remove the inner join part (and the reference to it in the order
part), and run the SQL on the server, then it works.

`inventory_software_installation`.`license_key_id` == NULL on the rows
in question (the model specifies null=True).

I have a suspicion that the above SQL doesn't work because it requires
a inventory_license object, however there is no inventory_license_key
object there is no inventory_license object.

The relevant definitions (I can provide more details on request) are:

class software_installation(models.Model):
[...]
software = models.ForeignKey(software)
[...]
license_key = models.ForeignKey(license_key,null=True,blank=True)
[...]

class license_key(models.Model):
[...]
license = models.ForeignKey(license)
[...]

class license(models.Model):
[...]


Any ideas?

Is this a django bug? If so is it a known issue?


Thanks

Brian May

Malcolm Tredinnick

unread,
Jan 14, 2009, 9:57:13 PM1/14/09
to django...@googlegroups.com

The SQL you've posted certainly looks wrong and I thought I'd spent
enough hours making sure we never constructed something like that. So
there's some edge-case you're hitting that isn't being catered for. No
big deal; we can fix that. However, the model fragments you've posted
don't support the SQL that's being generated. Thus, some of the stuff
you've trimmed is likely to be relevant. Filtering a
"software_installation" model on the "software" field isn't going to
involve selections on the license_key table at all.

Please open a ticket for this and assign it to me (mtredinnick in Trac),
since these tend to be the bugs that I get to fix. However, first,
pleaes create a small, complete example that demonstrates the problem in
a repeatable fashion. Simplest way to do that is to take your existing
models and start removing as many fields as possible until the problem
goes away. You can view the SQL that Django will generate by looking at

qs.query.as_sql()

where "qs" is any queryset (such as
software_installation.objects.filter(...)). You can't do this with
something that returns an immediate result (such as .count()), but
that's because count() doesn't return a queryset; it returns an integer.

The fragment you've posted above doesn't meet the "complete" definition,
since (a) it doesn't have anything for the "software" model, although
that's referred to and (b) if I was to create an empty software model
and run the query, license_key wouldn't show up in the query.

Regards,
Malcolm

Brian

unread,
Jan 14, 2009, 11:12:13 PM1/14/09
to Django users
On Jan 15, 1:57 pm, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:
> Please open a ticket for this and assign it to me (mtredinnick in Trac),

Ok, done. Thanks.

<http://code.djangoproject.com/ticket/10028>

Brian May
Reply all
Reply to author
Forward
0 new messages