subquery has too many columns

874 views
Skip to first unread message

timc3

unread,
Jan 7, 2009, 12:36:16 PM1/7/09
to Django users
I updated my django version to revision 9710 today and now I am
getting the following error message on screen:

Caught an exception while rendering: subquery has too many columns

This is the traceback:

Original Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/template/debug.py", line 71, in
render_node
result = node.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/template/debug.py", line 87, in render
output = force_unicode(self.filter_expression.resolve(context))
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/template/__init__.py", line 559, in
resolve
new_obj = func(obj, *arg_vals)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/template/defaultfilters.py", line 510,
in length
return len(value)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/db/models/query.py", line 160, in
__len__
self._result_cache = list(self.iterator())
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/db/models/query.py", line 275, in
iterator
for row in self.query.results_iter():
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/db/models/sql/query.py", line 203, in
results_iter
for rows in self.execute_sql(MULTI):
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/db/models/sql/query.py", line 1752, in
execute_sql
cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/site-packages/django/db/backends/util.py", line 19, in
execute
return self.cursor.execute(sql, params)
ProgrammingError: subquery has too many columns


It seems that this query is creating the problem:

groupmembers = requestedgroup.group_members.exclude
(id__in=groupadmins)

And the model looks like this:

group_members = models.ManyToManyField(User, verbose_name="group
members", related_name="groupofumembers")



And ideas?


Alex Koshelev

unread,
Jan 7, 2009, 1:26:39 PM1/7/09
to django...@googlegroups.com
And what is `groupadmins`?

Malcolm Tredinnick

unread,
Jan 7, 2009, 8:44:22 PM1/7/09
to django...@googlegroups.com
On Wed, 2009-01-07 at 09:36 -0800, timc3 wrote:
[...]

> ProgrammingError: subquery has too many columns
>
>
> It seems that this query is creating the problem:
>
> groupmembers = requestedgroup.group_members.exclude
> (id__in=groupadmins)

We'll need a bit more information here, since the devil's always in the
details. At a minimum, what is the output of

groupmembers.query.as_sql()

There may be a problem with exclude and nested querysets. I just
realised I haven't explicitly tested those.

Also, which database are you using? There are lots of SQL variations
between databases (for example, nested queries don't work with Oracle at
the moment for an unusual reason that I'll fix later today).

Regards,
Malcolm


timc3

unread,
Jan 8, 2009, 11:16:46 AM1/8/09
to Django users

>
> We'll need a bit more information here, since the devil's always in the
> details. At a minimum, what is the output of
>
>         groupmembers.query.as_sql()
>
> There may be a problem with exclude and nested querysets. I just
> realised I haven't explicitly tested those.
>
> Also, which database are you using? There are lots of SQL variations
> between databases (for example, nested queries don't work with Oracle at
> the moment for an unusual reason that I'll fix later today).

I am using PostgreSQL 8.3.4 on OS X and on Linux. The output of that
command is:

('SELECT "auth_user"."id", "auth_user"."username",
"auth_user"."first_name", "auth_user"."last_name",
"auth_user"."email", "auth_user"."password", "auth_user"."is_staff",
"auth_user"."is_active", "auth_user"."is_superuser",
"auth_user"."last_login", "auth_user"."date_joined" FROM "auth_user"
INNER JOIN "group_groupsofuser_group_members" ON ("auth_user"."id" =
"group_groupsofuser_group_members"."user_id") WHERE
("group_groupsofuser_group_members"."groupsofuser_id" = %s AND NOT
("auth_user"."id" IN (SELECT U0."id", U0."id" FROM "auth_user" U0
INNER JOIN "group_groupsofuser_group_admins" U1 ON (U0."id" =
U1."user_id") WHERE U1."groupsofuser_id" = %s )))',
(1, 1))

And my model looks like this:

class GroupsOfUser(models.Model):
"""
Groups containing users for allowing them to build communities
"""
name = models.CharField(_("Name"), max_length=50)
description = models.TextField(_("Description"), blank=True,
help_text=_("Optional"))
slug = models.SlugField()
group_members = models.ManyToManyField(User, verbose_name="group
members", related_name="groupofumembers")
group_admins = models.ManyToManyField(User, verbose_name="group
admins", related_name="groupofuadmins")
group_owner = models.ForeignKey(User, verbose_name="group owner",
related_name="groupofuowner")
date_created = models.DateTimeField(_("Creation date"),
auto_now_add=True)
closed_group = models.BooleanField(_("Closed group"),
default=False)
request_to_join = models.BooleanField(_("Users have to request to
join"), default=False)
adult_content = models.BooleanField(_("18 and above only"),
default=False)
group_picture = models.ImageField(_("Group image"),
upload_to="groupsimage/", blank=True, null=True)

def __str__(self):
return self.name

def save(self):
self.slug = slugify(self.name, instance=self)
super(GroupsOfUser, self).save()

class Meta:
ordering = ['-date_created']

def get_absolute_url(self):
return ('groupdetail', [str(self.slug)])
get_absolute_url = models.permalink(get_absolute_url)


In my views:

try:
requestedgroup = GroupsOfUser.objects.select_related().get
(slug=slug)
except GroupsOfUser.DoesNotExist:
objTemplate = loader.get_template('groups/
group_notfound.html')
objContext = RequestContext(request, {
'slug': slug,
})
return HttpResponse(objTemplate.render(objContext))

# Bring back the requests for this group we might be an
administrator for.
groupadminfor = request.user.groupofuadmins.all()
groupreq = GroupsOfUserRequest.objects.filter(to_group__in = [grp
for grp in groupadminfor]).filter(to_group =
requestedgroup).select_related()
isgroupadmin = request.user.groupofuadmins.filter(id =
requestedgroup.id)
ismember = request.user.groupofumembers.filter(id =
requestedgroup.id)

#Check to make sure that this isn't a closed group
if requestedgroup.closed_group == True and ismember.count() < 1:
logging.info('User trying to get to closed group')
raise Http404

groupadmins = requestedgroup.group_admins.values_list('id',
flat=True)
groupmembers = requestedgroup.group_members.exclude
(id__in=groupadmins)


Any help will be appreciated.
Reply all
Reply to author
Forward
0 new messages