Ticket #8901: postgresql-get-sequence-name.diff

File postgresql-get-sequence-name.diff, 3.0 KB (added by Lemuel Formacil, 15 years ago)

Added a get_sequence_name function that queries postgresql function pg_get_serial_sequence and updated last_insert_id, sql_flush, and sequence_reset_sql methods.

  • django/db/backends/postgresql/operations.py

     
    5353        return '%s'
    5454
    5555    def last_insert_id(self, cursor, table_name, pk_name):
    56         cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name))
     56        cursor.execute("SELECT CURRVAL('\"%s\"')" % get_sequence_name(table_name, pk_name))
    5757        return cursor.fetchone()[0]
    5858
    5959    def no_limit_value(self):
     
    9090                table_name = sequence_info['table']
    9191                column_name = sequence_info['column']
    9292                if column_name and len(column_name) > 0:
    93                     sequence_name = '%s_%s_seq' % (table_name, column_name)
     93                    sequence_name = get_sequence_name(table_name, column_name)
    9494                else:
    95                     sequence_name = '%s_id_seq' % table_name
     95                    sequence_name = get_sequence_name(table_name)
    9696                sql.append("%s setval('%s', 1, false);" % \
    9797                    (style.SQL_KEYWORD('SELECT'),
    9898                    style.SQL_FIELD(self.quote_name(sequence_name)))
     
    113113                if isinstance(f, models.AutoField):
    114114                    output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
    115115                        (style.SQL_KEYWORD('SELECT'),
    116                         style.SQL_FIELD(qn('%s_%s_seq' % (model._meta.db_table, f.column))),
     116                        style.SQL_FIELD(qn(get_sequence_name(model._meta.db_table, f.column))),
    117117                        style.SQL_FIELD(qn(f.column)),
    118118                        style.SQL_FIELD(qn(f.column)),
    119119                        style.SQL_KEYWORD('IS NOT'),
     
    124124                if not f.rel.through:
    125125                    output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
    126126                        (style.SQL_KEYWORD('SELECT'),
    127                         style.SQL_FIELD(qn('%s_id_seq' % f.m2m_db_table())),
     127                        style.SQL_FIELD(qn(get_sequence_name(f.m2m_db_table()))),
    128128                        style.SQL_FIELD(qn('id')),
    129129                        style.SQL_FIELD(qn('id')),
    130130                        style.SQL_KEYWORD('IS NOT'),
     
    162162            if self.postgres_version[0:2] == (8,2):
    163163                if self.postgres_version[2] is None or self.postgres_version[2] <= 4:
    164164                    raise NotImplementedError('PostgreSQL 8.2 to 8.2.4 is known to have a faulty implementation of %s. Please upgrade your version of PostgreSQL.' % aggregate.sql_function)
     165
     166
     167def get_sequence_name(table_name, field_name='id'):
     168    from django.db import connection
     169    cursor = connection.cursor()
     170    cursor.execute("SELECT pg_get_serial_sequence('%s', '%s')" % (table_name, field_name))
     171    seq = cursor.fetchone()[0]
     172    return seq.split('.')[1]
Back to Top