Ticket #8901: postgres_sequence_name.patch

File postgres_sequence_name.patch, 1.0 KB (added by adam@…, 16 years ago)
  • django/db/backends/postgresql/operations.py

     
    5555        return '%s'
    5656
    5757    def last_insert_id(self, cursor, table_name, pk_name):
    58         cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name))
     58        # This is tricky.  It's possible for len(seq_guts) to be exactly 59. 
     59        # That's not too long, and Postgres doesn't clip it.  But, if the
     60        # tablename is 28, and the pk_name is 30, (plus the '_'), then
     61        # unconditionally clipping both parts to 29 removes an extra char,
     62        # and we get a wrong answer.
     63        seq_guts = '%s_%s' % (table_name,pk_name)
     64        if len(seq_guts) > 59:
     65            seq_guts = "%s_%s" % (table_name[:29], pk_name[:29])
     66        cursor.execute("SELECT CURRVAL('\"%s_seq\"')" % (seq_guts))
    5967        return cursor.fetchone()[0]
    6068
    6169    def no_limit_value(self):
Back to Top