Ticket #1760: mysql_order_wrt_3.diff

File mysql_order_wrt_3.diff, 1.2 KB (added by Simon Litchfield <simon@…>, 17 years ago)

Patch against rev 6022 (uses new qn() function) Surely it's about time to commit this small patch? It's critical for MySQL users and been waiting over a year now

  • django/db/models/base.py

     
    241241            placeholders = ['%s'] * len(field_names)
    242242            if self._meta.order_with_respect_to:
    243243                field_names.append(qn('_order'))
    244                 # TODO: This assumes the database supports subqueries.
    245                 placeholders.append('(SELECT COUNT(*) FROM %s WHERE %s = %%s)' % \
    246                     (qn(self._meta.db_table), qn(self._meta.order_with_respect_to.column)))
    247                 db_values.append(getattr(self, self._meta.order_with_respect_to.attname))
     244                placeholders.append('%s')
     245                subsel = 'SELECT COUNT(*) FROM %s WHERE %s = %%s' % (
     246                    qn(self._meta.db_table),
     247                    qn(self._meta.order_with_respect_to.column))
     248                cursor.execute(subsel, (getattr(self, self._meta.order_with_respect_to.attname),))
     249                db_values.append(cursor.fetchone()[0])
    248250            if db_values:
    249251                cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \
    250252                    (qn(self._meta.db_table), ','.join(field_names),
Back to Top