From 86fd59cfa51fb703c9553d15717f5ef34b181dde Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sun, 10 Jun 2007 03:31:26 +0000 Subject: [PATCH] Fixed #4432 -- Fixed PostgreSQL sequence resetting in the case when a table has no rows yet. Thanks, mrmachine. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5455 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- AUTHORS | 1 + django/db/backends/postgresql/base.py | 13 +++++++++---- django/db/backends/postgresql_psycopg2/base.py | 13 +++++++++---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/AUTHORS b/AUTHORS index bff482f294..01f7983e36 100644 --- a/AUTHORS +++ b/AUTHORS @@ -170,6 +170,7 @@ answer newbie questions, and generally made Django that much better: mitakummaa@gmail.com mmarshall Eric Moritz + mrmachine Robin Munn Robert Myers Nebojša Dorđević diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index a5b38a883d..fedbb6b7f1 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -219,22 +219,27 @@ def get_sql_sequence_reset(style, model_list): from django.db import models output = [] for model in model_list: + # Use `coalesce` to set the sequence for each model to the max pk value if there are records, + # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true + # if there are records (as the max pk value is already in use), otherwise set it to false. for f in model._meta.fields: if isinstance(f, models.AutoField): - output.append("%s setval('%s', (%s max(%s) %s %s));" % \ + output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ (style.SQL_KEYWORD('SELECT'), style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))), - style.SQL_KEYWORD('SELECT'), style.SQL_FIELD(quote_name(f.column)), + style.SQL_FIELD(quote_name(f.column)), + style.SQL_KEYWORD('IS NOT'), style.SQL_KEYWORD('FROM'), style.SQL_TABLE(quote_name(model._meta.db_table)))) break # Only one AutoField is allowed per model, so don't bother continuing. for f in model._meta.many_to_many: - output.append("%s setval('%s', (%s max(%s) %s %s));" % \ + output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ (style.SQL_KEYWORD('SELECT'), style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())), - style.SQL_KEYWORD('SELECT'), style.SQL_FIELD(quote_name('id')), + style.SQL_FIELD(quote_name('id')), + style.SQL_KEYWORD('IS NOT'), style.SQL_KEYWORD('FROM'), style.SQL_TABLE(f.m2m_db_table()))) return output diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index 9898b121f4..d9ad363ac1 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -176,22 +176,27 @@ def get_sql_sequence_reset(style, model_list): from django.db import models output = [] for model in model_list: + # Use `coalesce` to set the sequence for each model to the max pk value if there are records, + # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true + # if there are records (as the max pk value is already in use), otherwise set it to false. for f in model._meta.fields: if isinstance(f, models.AutoField): - output.append("%s setval('%s', (%s max(%s) %s %s));" % \ + output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ (style.SQL_KEYWORD('SELECT'), style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))), - style.SQL_KEYWORD('SELECT'), style.SQL_FIELD(quote_name(f.column)), + style.SQL_FIELD(quote_name(f.column)), + style.SQL_KEYWORD('IS NOT'), style.SQL_KEYWORD('FROM'), style.SQL_TABLE(quote_name(model._meta.db_table)))) break # Only one AutoField is allowed per model, so don't bother continuing. for f in model._meta.many_to_many: - output.append("%s setval('%s', (%s max(%s) %s %s));" % \ + output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ (style.SQL_KEYWORD('SELECT'), style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())), - style.SQL_KEYWORD('SELECT'), style.SQL_FIELD(quote_name('id')), + style.SQL_FIELD(quote_name('id')), + style.SQL_KEYWORD('IS NOT'), style.SQL_KEYWORD('FROM'), style.SQL_TABLE(f.m2m_db_table()))) return output