#9188 closed (fixed)
Postgresql 'missing FROM-clause entry in subquery for table' error on lookup that spans relationships
Reported by: | naitsirhc | Owned by: | Malcolm Tredinnick |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.0 |
Severity: | Keywords: | Postgresql, join, relationships | |
Cc: | brent.hagany@…, erik@… | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This is a bug discussed in the Django users Google Group:
http://groups.google.com/group/django-users/browse_thread/thread/3940c77215fa9fa7?hl=en#
I'm encountering an error when performing a lookup that spans
relationships. The query is as follows:
myitems = MyItem.objects_all.exclude(user__somemodel__created__gte=(datetime.now()-timedelta(days=3)))
With the following (stripped) models:
class MyItem(models.Model): user = models.ForeignKey(user, unique=True) class SomeModel(models.Model): created = models.DateTimeField('Date created', default=datetime.now) user = models.ForeignKey(User)
The error I get is:
Traceback (most recent call last): .... File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 179, in _result_iter self._fill_cache() File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 612, in _fill_cache self._result_cache.append(self._iter.next()) File "/usr/lib/python2.5/site-packages/django/db/models/query.py", line 269, in iterator for row in self.query.results_iter(): File "/usr/lib/python2.5/site-packages/django/db/models/sql/ query.py", line 206, in results_iter for rows in self.execute_sql(MULTI): File "/usr/lib/python2.5/site-packages/django/db/models/sql/ query.py", line 1723, in execute_sql cursor.execute(sql, params) File "/usr/lib/python2.5/site-packages/django/db/backends/util.py", line 19, in execute return self.cursor.execute(sql, params) psycopg2.ProgrammingError: missing FROM-clause entry in subquery for table "u1" LINE 1: ..._myitem" U0 INNER JOIN "notes_note" U2 ON (U1."id" = ... ^
To debug the issue, I looked at the SQL generated by the 'myitems'
queryset above. The generated SQL is:
SELECT "users_myitem"."id", "users_myitem"."user_id" FROM "users_myitem" WHERE NOT ( "users_myitem"."user_id" IN ( SELECT U2."user_id" FROM "users_myitem" U0 INNER JOIN "myapp_somemodel" U2 ON (U1."id" = U2."user_id") WHERE U2."created" >= 2008-09-19 19:57:43.111687 ) ) }} It looks like the table "users_myitem" is being improperly labeled as 'U0', and then is referred to as 'U1' on the next line. If I correct this issue and run the SQL command manually, I get the expected records returned. The corrected SQL is as follows: {{{ SELECT "users_myitem"."id", "users_myitem"."user_id" FROM "users_myitem" WHERE NOT ( "users_myitem"."user_id" IN ( SELECT U2."user_id" FROM "users_myitem" U1 INNER JOIN "myapp_somemodel" U2 ON (U1."id" = U2."user_id") WHERE U2."created" >= '2008-09-19 19:51:43.151089' ) ) }}}
Attachments (1)
Change History (12)
comment:1 by , 16 years ago
Cc: | added |
---|
comment:2 by , 16 years ago
comment:3 by , 16 years ago
Cc: | added |
---|
follow-up: 5 comment:4 by , 16 years ago
As I mentioned in the original thread, this description doesn't contain all the information needed to replicate the problem. It refers to a "user" model that isn't described anywhere and uses a custom manager (object_all
). I can't repeat the bug using a normal manager and two models pointing to a third common model.
So I'll either need a complete set of models that is self-contained and replicates the problem or a patch against django/regressiontests/queries/models.py
that fails with the same problem. At the moment, I'm a bit stuck as to how to reveal the problem and it's not caused by what I thought might have been the issue.
comment:5 by , 16 years ago
Replying to mtredinnick:
As I mentioned in the original thread, this description doesn't contain all the information needed to replicate the problem. It refers to a "user" model that isn't described anywhere and uses a custom manager (
object_all
). I can't repeat the bug using a normal manager and two models pointing to a third common model.
So I'll either need a complete set of models that is self-contained and replicates the problem or a patch against
django/regressiontests/queries/models.py
that fails with the same problem. At the moment, I'm a bit stuck as to how to reveal the problem and it's not caused by what I thought might have been the issue.
This is the attachment I put on the ticket I opened about this (#9192), which includes three very simple models that replicate this issue. I haven't tried it against a recent revision, but I don't think any major changes have been made that would affect this behavior.
follow-up: 7 comment:6 by , 16 years ago
Excellent. Thanks, Brent. Sorry for forgetting about the example on the duplicate ticket. I don't know why my almost identical case didn't trigger this, but you win the prize for "simplest possible example." Fails reliably now (only in debugging is this considered a good thing).
comment:7 by , 16 years ago
Replying to mtredinnick:
Heh, I don't think anybody expects you to remember every duplicate ticket :). If you need anything else to fail all the time, just let me know. I seem to have a knack.
follow-up: 9 comment:8 by , 16 years ago
My problem turned out to be much more fundamental: my test case fails as expected when I remember to run it against trunk. I was accidentally running it against my local branch of SQL-exclude fixes where I have already fixed this problem just from code inspection.
comment:10 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
The same error occurs with sqlite as the backend db.