Opened 4 years ago

Closed 4 years ago

#31401 closed Bug (duplicate)

Exclude on M2M with F-function not working.

Reported by: Ron Owned by: nobody
Component: Database layer (models, ORM) Version: 2.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Mariusz Felisiak)

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(projectassignees=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

Change History (1)

comment:1 by Mariusz Felisiak, 4 years ago

Description: modified (diff)
Resolution: duplicate
Status: newclosed
Summary: Exclude on M2M with F-function not workingExclude on M2M with F-function not working.

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

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