Django

Code

Ticket #1760: mysql_order_wrt.diff

File mysql_order_wrt.diff, 1.8 kB (added by Christopher Lenz <cmlenz@gmx.de>, 4 years ago)

Hack to workaround the problem

  • django/db/models/base.py

    old new  
    176176            placeholders = ['%s'] * len(field_names) 
    177177            if self._meta.order_with_respect_to: 
    178178                field_names.append(backend.quote_name('_order')) 
    179                 # TODO: This assumes the database supports subqueries. 
    180                 placeholders.append('(SELECT COUNT(*) FROM %s WHERE %s = %%s)' % \ 
    181                     (backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.order_with_respect_to.column))) 
    182                 db_values.append(getattr(self, self._meta.order_with_respect_to.attname)) 
     179                if settings.DATABASE_ENGINE == 'mysql': 
     180                    placeholders.append('%s') 
     181                    subsel = 'SELECT COUNT(*) FROM %s WHERE %s = %%s' % ( 
     182                        backend.quote_name(self._meta.db_table), 
     183                        backend.quote_name(self._meta.order_with_respect_to.column)) 
     184                    cursor.execute(subsel, (getattr(self, self._meta.order_with_respect_to.attname),)) 
     185                    db_values.append(cursor.fetchone()[0]) 
     186                else: 
     187                    # TODO: This assumes the database supports subqueries. 
     188                    placeholders.append('(SELECT COUNT(*) FROM %s WHERE %s = %%s)' % \ 
     189                        (backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.order_with_respect_to.column))) 
     190                    db_values.append(getattr(self, self._meta.order_with_respect_to.attname)) 
    183191            cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \ 
    184192                (backend.quote_name(self._meta.db_table), ','.join(field_names), 
    185193                ','.join(placeholders)), db_values)