deschler / django-modeltranslation

Translates Django models using a registration approach.
BSD 3-Clause "New" or "Revised" License
1.34k stars 257 forks source link

Crash when a model with multi-table inheritance also has default ordering #703

Open ryselis opened 9 months ago

ryselis commented 9 months ago

Consider the following models:

class OrderGood(models.Model):
    pass

class SupplyOrderGoodServiceBase(models.Model):
    supplier_price = models.DecimalField(max_digits=12, decimal_places=4)
    title = models.CharField(max_length=512)

    class Meta:
        ordering = ('id',)

class SupplyOrderGood(SupplyOrderGoodServiceBase):
    order_good = models.ForeignKey(to=OrderGood, on_delete=models.CASCADE)

Title field must be translated, the options are configured as follows:

@register(SupplyOrderGoodServiceBase)
class SupplyOrderGoodServiceBaseTranslation(TranslationOptions):
    fields = ('title',)

@register(SupplyOrderGood)
class SupplyOrderGoodServiceBaseTranslation(TranslationOptions):
    fields = ()

However, update queries do not work due to default ordering being added to order_by. Consider this query:

SupplyOrderGood.objects.filter(order_good_id=self.order_good.pk).update(supplier_price=Decimal(6))

It generates the following query:

 ORDER BY `supply_supplyordergood`.`supplyordergoodservicebase_ptr_id` ASC

I believe this happens only on MySQL as the incompatibility comes from SQLUpdateCompiler class for MySQL. update_query and update_params are both empty, but self.query.order_by is not, which leads to this query with only the ORDER BY part.

I uploaded an example project here https://github.com/ryselis/django_34495 There is a failing test in supply/tests.py

last-partizan commented 8 months ago

Hi, thanks for the report.

If you can trace it deeper and make PR with a fix, i can merge it.