Opened 3 years ago
Closed 3 years ago
#34495 closed Bug (worksforme)
Queryset update fails when updating parent model field with default ordering on MySQL backend
| Reported by: | Karolis Ryselis | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 3.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
consider the following models:
class SupplyOrderGoodServiceBase(models.Model):
supplier_price = models.DecimalField(max_digits=12, decimal_places=4)
class Meta:
ordering = ('id',)
class SupplyOrderGood(SupplyOrderGoodServiceBase):
order_good = models.ForeignKey(to="sales.OrderGood", on_delete=models.CASCADE)
Running the following query fails:
SupplyOrderGood.objects.filter(order_good_id=order_good.pk).update(supplier_price=supplier_price)
This happens because the SupplyOrderGood instances do not have to be updated, the update is on SupplyOrderGoodServiceBase. SQLUpdateCompiler generates no SQL for SupplyOrderGood (as_sql returns '', []). However, MySQL's SQLUpdateCompiler finds self.query.order_by to be not empty and adds ORDER BY to an empty SQL query. Therefore, it tries to run SQL query ORDER BY sales_supplyordergood.supplyordergoodservicebase_ptr_id ASC, which, of course, fails.
This can be worked around by added an .order_by() to the query.
Thanks for this ticket, however it works for me. Can you provide a sample project that reproduces the issue? Can you reproduce it on Django 4.2?