diff options
author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-06-11 14:51:26 +0000 |
---|---|---|
committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2007-06-11 14:51:26 +0000 |
commit | 748ea65a6180293c356faa3ff0e754cb1d678d14 (patch) | |
tree | 542a590e521cfd639d1903a4968e75edd68e039d /django/db | |
parent | 0b4122d1387e3c793661c0828278a021364691d4 (diff) | |
download | django-748ea65a6180293c356faa3ff0e754cb1d678d14.tar.gz |
unicode: Merged changes from trunk up to [5460].
git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5462 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db')
-rw-r--r-- | django/db/backends/postgresql/base.py | 13 | ||||
-rw-r--r-- | django/db/backends/postgresql_psycopg2/base.py | 13 | ||||
-rw-r--r-- | django/db/backends/util.py | 2 |
3 files changed, 19 insertions, 9 deletions
diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index 912d49b493..2186fa3863 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -239,22 +239,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 d9a850957c..764e3a34f3 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -180,22 +180,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/util.py b/django/db/backends/util.py index 9f75ff2844..fd0f6598a7 100644 --- a/django/db/backends/util.py +++ b/django/db/backends/util.py @@ -98,7 +98,7 @@ def typecast_boolean(s): return str(s)[0].lower() == 't' def typecast_decimal(s): - if s is None: + if s is None or s == '': return None return decimal.Decimal(s) |