Ticket #13821: pg_fix_get_serial_sequence.diff

File pg_fix_get_serial_sequence.diff, 2.5 KB (added by Paul McMillan, 14 years ago)
  • django/db/backends/postgresql/operations.py

     
    5656    def last_insert_id(self, cursor, table_name, pk_name):
    5757        # Use pg_get_serial_sequence to get the underlying sequence name
    5858        # 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))
    6060        return cursor.fetchone()[0]
    6161
    6262    def no_limit_value(self):
     
    9696                    # This will be the case if it's an m2m using an autogenerated
    9797                    # intermediate table (see BaseDatabaseIntrospection.sequence_list)
    9898                    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);" % \
    100100                    (style.SQL_KEYWORD('SELECT'),
    101101                    style.SQL_TABLE(table_name),
    102102                    style.SQL_FIELD(column_name))
     
    118118
    119119            for f in model._meta.local_fields:
    120120                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;" % \
    122122                        (style.SQL_KEYWORD('SELECT'),
    123123                        style.SQL_TABLE(model._meta.db_table),
    124124                        style.SQL_FIELD(f.column),
     
    130130                    break # Only one AutoField is allowed per model, so don't bother continuing.
    131131            for f in model._meta.many_to_many:
    132132                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;" % \
    134134                        (style.SQL_KEYWORD('SELECT'),
    135135                        style.SQL_TABLE(model._meta.db_table),
    136136                        style.SQL_FIELD('id'),
Back to Top