Django

Code

Ticket #9985 (closed: fixed)

Opened 6 months ago

Last modified 4 months ago

r9701 breaks for combinations of values_list and unique - it inserts duplicate references in SQL

Reported by: cmutel@gmail.com Assigned to: mtredinnick
Milestone: Component: Database layer (models, ORM)
Version: SVN Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

r9701 introduces a change that leads to malformed SQL in queries that include values_list and distinct queries, at least in SQLite. A simple example:

class Database(models.Model):
    name = models.TextField()

class Process(models.Model):
    database = models.ForeignKey(Database)
    name = models.TextField()

The query

Database.objects.filter(id__in=Process.objects.filter(id__in=(1,2)).values_list(
'database', flat=True).distinct()) 

produces the following SQL:

SELECT "basic_database"."id", "basic_database"."name" FROM "basic_database" WHERE 
"basic_database"."id" IN (SELECT DISTINCT U0."database_id", U0."database_id" FROM 
"basic_process" U0 WHERE U0."id" IN (1, 2)) LIMIT 21

where U0."database_id" is repeated, and produces an error:

OperationalError: only a single result allowed for a SELECT that is part of an expression

Unfortunately, this is the limit of my Python knowledge, so I don't have a patch to correct this problem.

The query functions perfectly in r9700 and earlier.

Attachments

Change History

01/07/09 22:22:31 changed by mtredinnick

  • owner changed from nobody to mtredinnick.
  • needs_better_patch changed.
  • status changed from new to assigned.
  • needs_tests changed.
  • needs_docs changed.

That query cannot possibly come from the queryset you specify, since that queryset will never generate a limit. It's close, though, and does demonstrate a problem.

The root issue is that qs.values_list(...).values(....) doesn't work properly, which needs fixing.

01/07/09 23:49:07 changed by mtredinnick

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [9717]) Fixed #9985 -- qs.values_list(...).values(...) was constructing incorrect SQL.

02/25/09 13:51:44 changed by

  • milestone deleted.

Milestone post-1.0 deleted


Add/Change #9985 (r9701 breaks for combinations of values_list and unique - it inserts duplicate references in SQL)




Change Properties
Action