Cartridge Sale update mysql problem

Showing 1-2 of 2 messages
Cartridge Sale update mysql problem dodgyville 9/18/11 7:35 PM
Greetings,

When creating a Sale object in the mezzanine admin, I get:

OperationalError at /admin/shop/sale/3/
(1093, "You can't specify target table 'shop_product' for update in
FROM clause")

In cartridge/shop/models.py in Sale.save around line 731 (your version
may vary).
               priced_objects.filter(**extra_filter).update(**update)

Tthere is a known limitation with mysql that won't let you use the
same table for both the subquery FROM clause and the update target.
http://dev.mysql.com/doc/refman/5.0/en/subquery-errors.html

My hack solution is to "downgrade" that line from an update to a slow
loop through and save when encountering the mysql error, but a much
better solution would be to rewrite the SQL update command to work in
mysql too.

diff -r a68623abf7a5 cartridge/shop/models.py
--- a/cartridge/shop/models.py        Fri Sep 16 14:52:32 2011 +1000
+++ b/cartridge/shop/models.py        Mon Sep 19 12:33:30 2011 +1000
@@ -10,6 +10,7 @@
 from django.db.models.signals import post_save
 from django.dispatch import receiver
 from django.utils.translation import ugettext_lazy as _
+from _mysql_exceptions import OperationalError

 from countries.models import Country

@@ -729,6 +730,13 @@
                               "sale_to": self.valid_to,
                               "sale_from": self.valid_from}
                     priced_objects.filter(**extra_filter).update(**update)
+                except OperationalError: #work around for mysql
+                    try:
+                        for priced_object in
priced_objects.filter(**extra_filter):
+                            priced_object.__dict__.update(**update)
+                            priced_object.save()
+                    except Warning:
+                        pass
                 except Warning:
                     pass

Re: [mezzanine-users] Cartridge Sale update mysql problem Stephen McDonald 9/20/11 1:22 PM
Thanks Luke - if you'd like to commit a fix for this I'll merge it in. Might need a check around the import as well in case the mysql libs aren't installed (eg for people using postgres)--
Stephen McDonald