#28787: QuerySet.update() fails on MySQL if a subquery references the base table -------------------------------------+------------------------------------- Reporter: Paulo | Owner: nobody Type: Bug | Status: new Component: Database layer | Version: 1.11 (models, ORM) | Severity: Normal | Resolution: Keywords: | Triage Stage: Accepted Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- Changes (by Tim Graham):
* component: Uncategorized => Database layer (models, ORM) * stage: Unreviewed => Accepted * type: Uncategorized => Bug Old description: > Given the models below: > > {{{ > class House(models.Model): > name = models.CharField(max_length=200) > > class Room(models.Model): > name = models.CharField(max_length=200) > house = models.ForeignKey(House, on_delete=models.CASCADE) > }}} > > The following query fails: > > {{{ > houses_with_suites = House.objects.filter(room__name__icontains='suite') > Room.objects.filter(house__in=houses_with_suites).update(name='suite') > }}} > > With this error: > > {{{ > OperationalError: (1093, "You can't specify target table > 'mysql_test_room' for update in FROM clause") > }}} > > I think this has been around for a while > (https://code.djangoproject.com/ticket/20300). > > I've confirmed this only happens in MySQL. It works on PostgresSQL and > SQLite. New description: Given the models below: {{{ class House(models.Model): name = models.CharField(max_length=200) class Room(models.Model): name = models.CharField(max_length=200) house = models.ForeignKey(House, on_delete=models.CASCADE) }}} The following query fails: {{{ houses_with_suites = House.objects.filter(room__name__icontains='suite') Room.objects.filter(house__in=houses_with_suites).update(name='suite') }}} With this error: {{{ OperationalError: (1093, "You can't specify target table 'mysql_test_room' for update in FROM clause") }}} I think this has been around for a while (#20300). I've confirmed this only happens in MySQL. It works on PostgresSQL and SQLite. -- -- Ticket URL: <https://code.djangoproject.com/ticket/28787#comment:2> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.1a5228d4d62852384065c1c62f3556f6%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.