Opened 7 years ago

Closed 3 years ago

#7907 closed New feature (wontfix)

tables argument of QuerySet.extra should be allow a subquery.

Reported by: bear330 Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: queryset, extra, qn, subquery, post-qsrf
Cc: sciyoshi@…, shaun@… Triage Stage: Design decision needed
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If I call extra with tables parameter:

queryset.extra(tables=['(select * from table) as k'])

The result SQL will be:

select ........ from `self_table`, `(select * from table) as k`

the subquery will be invalid.

That will be nice if we can determine whether we need qn or not.
For a normal table:

queryset.extra(tables=[qn('tableA'), qn('tableB')])

But for a subquery I can just omit qn to do that.

Thanks.

Attachments (2)

7907.patch (1.7 KB) - added by sciyoshi 7 years ago.
7907_2.diff (1.9 KB) - added by shauncutts 5 years ago.
update of the previous patch -- conforms to new sql compilation

Download all attachments as: .zip

Change History (7)

comment:1 Changed 7 years ago by adamfast

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 7 years ago by sciyoshi

  • Cc sciyoshi@… added
  • Component changed from Core framework to Database wrapper
  • Has patch set
  • Keywords post-qsrf added
  • Needs documentation set
  • Needs tests set

Attaching a patch for one possible solution: allow the tables argument to be a dictionary-like object mapping table names to subqueries, where the subquery is not quoted. Your example could therefore be re-written as

queryset.extra(tables={
    'k': '(select * from table)'
})

Changed 7 years ago by sciyoshi

comment:3 Changed 5 years ago by shauncutts

  • Cc shaun@… added

Changed 5 years ago by shauncutts

update of the previous patch -- conforms to new sql compilation

comment:4 Changed 4 years ago by lukeplant

  • Severity set to Normal
  • Type set to New feature

comment:5 Changed 3 years ago by jacob

  • Easy pickings unset
  • Resolution set to wontfix
  • Status changed from new to closed
  • UI/UX unset

Now that we've got .raw(), .extra() should be used less and less. I'm against adding any more abilities to it -- it's a bad feature and should be used as little as possible.

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