Ticket #8901: postgresql-get-sequence-name.diff
File postgresql-get-sequence-name.diff, 3.0 KB (added by , 15 years ago) |
---|
-
django/db/backends/postgresql/operations.py
53 53 return '%s' 54 54 55 55 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)) 57 57 return cursor.fetchone()[0] 58 58 59 59 def no_limit_value(self): … … 90 90 table_name = sequence_info['table'] 91 91 column_name = sequence_info['column'] 92 92 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) 94 94 else: 95 sequence_name = '%s_id_seq' % table_name95 sequence_name = get_sequence_name(table_name) 96 96 sql.append("%s setval('%s', 1, false);" % \ 97 97 (style.SQL_KEYWORD('SELECT'), 98 98 style.SQL_FIELD(self.quote_name(sequence_name))) … … 113 113 if isinstance(f, models.AutoField): 114 114 output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ 115 115 (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))), 117 117 style.SQL_FIELD(qn(f.column)), 118 118 style.SQL_FIELD(qn(f.column)), 119 119 style.SQL_KEYWORD('IS NOT'), … … 124 124 if not f.rel.through: 125 125 output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ 126 126 (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()))), 128 128 style.SQL_FIELD(qn('id')), 129 129 style.SQL_FIELD(qn('id')), 130 130 style.SQL_KEYWORD('IS NOT'), … … 162 162 if self.postgres_version[0:2] == (8,2): 163 163 if self.postgres_version[2] is None or self.postgres_version[2] <= 4: 164 164 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 167 def 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]