﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
17284	last_insert_id in django.db.backends.postgresql.operations.py	EErlo	nobody	"The new version of function '''last_insert_id''' uses the ""new"" postgres( >= version 8) method to get the sequence of a table (pg_get_serial_sequence), but in postgres 8.1.23(so far) this method isn't 100% good.
In some rare situations, postgres returns NULL, returning None to a saved new model pk, making lots of problems, even on Django Admin Interface, when you need the new-created pk to do something after call the save() method, as the Admin Interface Auth User does.  I have made a treatment in django.db.backends.postgresql.operations.py, in the last_insert_id function:



{{{
    def last_insert_id(self, cursor, table_name, pk_name):
        # Use pg_get_serial_sequence to get the underlying sequence name
        # from the table name and column name (available since PostgreSQL 8)
        cursor.execute(""SELECT CURRVAL(pg_get_serial_sequence('%s','%s'))"" % (
            self.quote_name(table_name), pk_name))
        to_return = cursor.fetchone()[0]
        if not to_return or to_return == 'None':

            cursor.execute(""select substr(column_default,10,position('::regclass' in column_default)-11) from information_schema.columns where table_name = '%s' and column_name = '%s'"" % (table_name, pk_name))
            seq_name = cursor.fetchone()[0]
            cursor.execute(""SELECT CURRVAL('%s')""%(seq_name))
            to_return = cursor.fetchone()[0]
        return to_return
}}}


I wait for your answer to know if it will be added to the django next release."	Bug	closed	Database layer (models, ORM)	1.3	Normal	wontfix	postgres 8.1.23,ORM save		Accepted	0	0	0	0	0	0
