Code

Opened 7 years ago

Closed 7 years ago

#3355 closed (invalid)

filter() with related table stomps on extra(tables)

Reported by: Rob Hudson <treborhudson@…> Owned by: adrian
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: treborhudson@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I started a discussion here with no responses so I thought I'd file a bug so this doesn't get lost...
http://groups.google.com/group/django-developers/browse_thread/thread/a88046d26172908d/?hl=en#

Summary: If you're using extra() to specify extra tables to add to your query AND you're using filter() with a related table, the extra() table gets lost. Example (also found in the above message):

This correctly adds "page_item" to the FROM clause by using extra(). Notice I'm not yet referencing a related table in the filter() clause since template_id is contained in the Page object.

In [29]: Page.objects.extra(
                tables=['page_item'],
                where=['page_item.page_id=page_page.id'])
                .filter(template__id='1')._get_sql_clause()
Out[29]:
(['`page_page`.`id`',
   '`page_page`.`number`',
   '`page_page`.`title`',
   '`page_page`.`template_id`',
   '`page_page`.`description`'],
  ' FROM `page_page` , `page_item` WHERE page_item.page_id=page_page.id
AND (`page_page`.`template_id` = %s)',
  ['1'])

But adding the related table lookup for the 'name' column in the filter() clause breaks the extra() tables and "page_item" disappears...

In [30]: Page.objects.extra(
                tables=['page_item'],
                where=['page_item.page_id=page_page.id'])
                .filter(template__name='Default Template')
                ._get_sql_clause()
Out[30]:
(['`page_page`.`id`',
   '`page_page`.`number`',
   '`page_page`.`title`',
   '`page_page`.`template_id`',
   '`page_page`.`description`'],
  ' FROM `page_page` INNER JOIN `page_template` AS `page_page__template`
ON `page_page`.`template_id` = `page_page__template`.`id` , `page_item`
WHERE page_item.page_id=page_page.id AND (`page_page__template`.`name` =
%s)',
  ['Default Template']) 

Attachments (0)

Change History (2)

comment:1 Changed 7 years ago by Simon G. <dev@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 7 years ago by Rob Hudson <treborhudson@…>

  • Resolution set to invalid
  • Status changed from new to closed

This is my ticket but now, 6-7 months later, I don't see anything wrong here. The page_item stuff is plainly there. I'm going to close as invalid.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.