[Django] #31401: Exclude on M2M with F-function not working

3 views
Skip to first unread message

Django

unread,
Mar 25, 2020, 11:08:12 AM3/25/20
to django-...@googlegroups.com
#31401: Exclude on M2M with F-function not working
-------------------------------------+-------------------------------------
Reporter: Ron | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 2.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I have the following models:

class Activity(models.Model):
user = models.ForeignKey(User, related_name='activities')
project = models.ForeignKey(Project, related_name='activities')


class Project(models.Model):
assignees = models.ManyToManyField(User, related_name='projects')

Now I want to query for all the activities which belog to a user which is
NOT in the projects assignees.

My query:

Activity.objects.exclude(project__assignees=F('user'))

Problem is, I always get this error:

> django.db.utils.OperationalError: (1054, "Unknown column 'U2.id' in 'on
clause'")

Im working on django 2.2.11 and MySQL.

I found a couple of [old django bugs][1] but they are supposed to be fixed
since ages.

Any ideas how I can solve my problem? My activity table is huge and I need
an efficient way. And I'd be happy to not use raw queries.

Thanks!


[1]: https://code.djangoproject.com/ticket/28551

--
Ticket URL: <https://code.djangoproject.com/ticket/31401>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 25, 2020, 3:03:13 PM3/25/20
to django-...@googlegroups.com
#31401: Exclude on M2M with F-function not working.

-------------------------------------+-------------------------------------
Reporter: Ron | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => duplicate


Old description:

> I have the following models:
>
> class Activity(models.Model):
> user = models.ForeignKey(User, related_name='activities')
> project = models.ForeignKey(Project, related_name='activities')
>

> class Project(models.Model):
> assignees = models.ManyToManyField(User, related_name='projects')
>
> Now I want to query for all the activities which belog to a user which is
> NOT in the projects assignees.
>
> My query:
>
> Activity.objects.exclude(project__assignees=F('user'))
>
> Problem is, I always get this error:
>
> > django.db.utils.OperationalError: (1054, "Unknown column 'U2.id' in 'on
> clause'")
>
> Im working on django 2.2.11 and MySQL.
>
> I found a couple of [old django bugs][1] but they are supposed to be
> fixed since ages.
>
> Any ideas how I can solve my problem? My activity table is huge and I
> need an efficient way. And I'd be happy to not use raw queries.
>
> Thanks!
>

> [1]: https://code.djangoproject.com/ticket/28551

New description:

I have the following models:
{{{
class Activity(models.Model):
user = models.ForeignKey(User, related_name='activities')
project = models.ForeignKey(Project, related_name='activities')


class Project(models.Model):
assignees = models.ManyToManyField(User, related_name='projects')
}}}
Now I want to query for all the activities which belog to a user which is
NOT in the projects assignees.

My query:

Activity.objects.exclude(project__assignees=F('user'))

Problem is, I always get this error:

> django.db.utils.OperationalError: (1054, "Unknown column 'U2.id' in 'on
clause'")

Im working on django 2.2.11 and MySQL.

I found a couple of [old django bugs][1] but they are supposed to be fixed
since ages.

Any ideas how I can solve my problem? My activity table is huge and I need
an efficient way. And I'd be happy to not use raw queries.

Thanks!


[1]: https://code.djangoproject.com/ticket/28551

--

Comment:

Duplicate of #21703 ( that was fixed in Django 3.0).

--
Ticket URL: <https://code.djangoproject.com/ticket/31401#comment:1>

Reply all
Reply to author
Forward
0 new messages