I recently had much fun migrating a large Django project from Django 1.0 to 1.3. During the migration a pretty tricky bug popped up. I had a listener attached to the post_save signal for the User object, which created some additional objects upon registration. The problem was, after the upgrade to Django 1.3, the object instance’s id was set to None when passed to the post_save handler.
After diving down the Django db stack I concluded that a newly introduced method of retrieving the last inserted id from my Postgresql database was failing. The SQL used is:
SELECT CURRVAL(pg_get_serial_sequence(‘auth_user’,’id’))
but for some reason pg_get_serial_sequence was returning NULL - so the id could not be retrieved.
Googling hinted that this was because the sequence didn’t have the correct table ownership. Indeed, performing this query on the database:
ALTER SEQUENCE auth_user_id_seq OWNED BY auth_user.id;
solved the issue!
My database is very old; the production database was probably created by Django 0.96 or earlier. The correct sequence ownership where either never set up correctly or lost somewhere along the way.







