Ticket #13821: pg_fix_get_serial_sequence.diff
File pg_fix_get_serial_sequence.diff, 2.5 KB (added by , 14 years ago) |
---|
-
django/db/backends/postgresql/operations.py
56 56 def last_insert_id(self, cursor, table_name, pk_name): 57 57 # Use pg_get_serial_sequence to get the underlying sequence name 58 58 # from the table name and column name (available since PostgreSQL 8) 59 cursor.execute("SELECT CURRVAL(pg_get_serial_sequence(' %s','%s'))" % (table_name, pk_name))59 cursor.execute("SELECT CURRVAL(pg_get_serial_sequence('\"%s\"','%s'))" % (table_name, pk_name)) 60 60 return cursor.fetchone()[0] 61 61 62 62 def no_limit_value(self): … … 96 96 # This will be the case if it's an m2m using an autogenerated 97 97 # intermediate table (see BaseDatabaseIntrospection.sequence_list) 98 98 column_name = 'id' 99 sql.append("%s setval(pg_get_serial_sequence(' %s','%s'), 1, false);" % \99 sql.append("%s setval(pg_get_serial_sequence('\"%s\"','%s'), 1, false);" % \ 100 100 (style.SQL_KEYWORD('SELECT'), 101 101 style.SQL_TABLE(table_name), 102 102 style.SQL_FIELD(column_name)) … … 118 118 119 119 for f in model._meta.local_fields: 120 120 if isinstance(f, models.AutoField): 121 output.append("%s setval(pg_get_serial_sequence(' %s','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \121 output.append("%s setval(pg_get_serial_sequence('\"%s\"','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ 122 122 (style.SQL_KEYWORD('SELECT'), 123 123 style.SQL_TABLE(model._meta.db_table), 124 124 style.SQL_FIELD(f.column), … … 130 130 break # Only one AutoField is allowed per model, so don't bother continuing. 131 131 for f in model._meta.many_to_many: 132 132 if not f.rel.through: 133 output.append("%s setval(pg_get_serial_sequence(' %s','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \133 output.append("%s setval(pg_get_serial_sequence('\"%s\"','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ 134 134 (style.SQL_KEYWORD('SELECT'), 135 135 style.SQL_TABLE(model._meta.db_table), 136 136 style.SQL_FIELD('id'),