Skip to content

Commit

Permalink
Fixed #3790 -- Fixed a problem with sequence resetting during fixture…
Browse files Browse the repository at this point in the history
… loads when using Postgres. Thanks to Jon Ballard and scott@staplefish.com for the report, and to Zach Thompson for suggesting a solution.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4937 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Apr 6, 2007
1 parent e339a41 commit dabd966
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 26 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ answer newbie questions, and generally made Django that much better:
Ville Säävuori <http://www.unessa.net/>
Tyson Tate <tyson@fallingbullets.com>
thebjorn <bp@datakortet.no>
Zach Thompson <zthompson47@gmail.com>
Tom Tobin
Joe Topjian <http://joe.terrarum.net/geek/code/python/django/>
torne-django@wolfpuppy.org.uk
Expand Down
38 changes: 13 additions & 25 deletions django/core/management.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,30 +410,10 @@ def get_sql_initial_data(apps):
get_sql_initial_data.args = ''

def get_sql_sequence_reset(app):
"Returns a list of the SQL statements to reset PostgreSQL sequences for the given app."
"Returns a list of the SQL statements to reset sequences for the given app."
from django.db import backend, models
output = []
for model in models.get_models(app):
for f in model._meta.fields:
if isinstance(f, models.AutoField):
output.append("%s setval('%s', (%s max(%s) %s %s));" % \
(style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD('%s_%s_seq' % (model._meta.db_table, f.column)),
style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD(backend.quote_name(f.column)),
style.SQL_KEYWORD('FROM'),
style.SQL_TABLE(backend.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));" % \
(style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD('%s_id_seq' % f.m2m_db_table()),
style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD(backend.quote_name('id')),
style.SQL_KEYWORD('FROM'),
style.SQL_TABLE(f.m2m_db_table())))
return output
get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting PostgreSQL sequences for the given app name(s)."
return backend.get_sql_sequence_reset(style, models.get_models(app))
get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting sequences for the given app name(s)."
get_sql_sequence_reset.args = APP_ARGS

def get_sql_indexes(app):
Expand Down Expand Up @@ -1333,13 +1313,14 @@ def load_data(fixture_labels, verbosity=1):
"Installs the provided fixture file(s) as data in the database."
from django.db.models import get_apps
from django.core import serializers
from django.db import connection, transaction
from django.db import connection, transaction, backend
from django.conf import settings
import sys

# Keep a count of the installed objects and fixtures
count = [0,0]

models = set()

humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path'

# Get a cursor (even though we don't need one yet). This has
Expand Down Expand Up @@ -1401,6 +1382,7 @@ def load_data(fixture_labels, verbosity=1):
objects = serializers.deserialize(format, fixture)
for obj in objects:
count[0] += 1
models.add(obj.object.__class__)
obj.save()
label_found = True
except Exception, e:
Expand All @@ -1422,6 +1404,12 @@ def load_data(fixture_labels, verbosity=1):
else:
if verbosity > 0:
print "Installed %d object(s) from %d fixture(s)" % tuple(count)
sequence_sql = backend.get_sql_sequence_reset(style, models)
if sequence_sql:
if verbosity > 1:
print "Resetting sequences"
for line in sequence_sql:
cursor.execute(line)
transaction.commit()
transaction.leave_transaction_management()

Expand Down
5 changes: 5 additions & 0 deletions django/db/backends/ado_mssql/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ def get_sql_flush(sql_styler, full_table_list):
sql_styler.SQL_FIELD(quote_name(table))
) for table in full_table_list]

def get_sql_sequence_reset(style, model_list):
"Returns a list of the SQL statements to reset sequences for the given models."
# No sequence reset required
return []

OPERATOR_MAPPING = {
'exact': '= %s',
'iexact': 'LIKE %s',
Expand Down
1 change: 1 addition & 0 deletions django/db/backends/dummy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ def close(self):
get_fulltext_search_sql = complain
get_drop_foreignkey_sql = complain
get_sql_flush = complain
get_sql_sequence_reset = complain

OPERATOR_MAPPING = {}
5 changes: 5 additions & 0 deletions django/db/backends/mysql/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@ def get_sql_flush(style, tables, sequences):
else:
return []

def get_sql_sequence_reset(style, model_list):
"Returns a list of the SQL statements to reset sequences for the given models."
# No sequence reset required
return []

OPERATOR_MAPPING = {
'exact': '= %s',
'iexact': 'LIKE %s',
Expand Down
5 changes: 5 additions & 0 deletions django/db/backends/mysql_old/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ def get_sql_flush(style, tables, sequences):
else:
return []

def get_sql_sequence_reset(style, model_list):
"Returns a list of the SQL statements to reset sequences for the given models."
# No sequence reset required
return []

OPERATOR_MAPPING = {
'exact': '= %s',
'iexact': 'LIKE %s',
Expand Down
4 changes: 4 additions & 0 deletions django/db/backends/oracle/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ def get_sql_flush(style, tables, sequences):
style.SQL_FIELD(quote_name(table))
) for table in tables]

def get_sql_sequence_reset(style, model_list):
"Returns a list of the SQL statements to reset sequences for the given models."
# No sequence reset required
return []

OPERATOR_MAPPING = {
'exact': '= %s',
Expand Down
24 changes: 24 additions & 0 deletions django/db/backends/postgresql/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,30 @@ def get_sql_flush(style, tables, sequences):
else:
return []

def get_sql_sequence_reset(style, model_list):
"Returns a list of the SQL statements to reset sequences for the given models."
from django.db import models
output = []
for model in model_list:
for f in model._meta.fields:
if isinstance(f, models.AutoField):
output.append("%s setval('%s', (%s max(%s) %s %s));" % \
(style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD('%s_%s_seq' % (model._meta.db_table, f.column)),
style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD(quote_name(f.column)),
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));" % \
(style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD('%s_id_seq' % f.m2m_db_table()),
style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD(quote_name('id')),
style.SQL_KEYWORD('FROM'),
style.SQL_TABLE(f.m2m_db_table())))
return output

# Register these custom typecasts, because Django expects dates/times to be
# in Python's native (standard-library) datetime/time format, whereas psycopg
Expand Down
25 changes: 25 additions & 0 deletions django/db/backends/postgresql_psycopg2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,31 @@ def get_sql_flush(style, tables, sequences):
return sql
else:
return []

def get_sql_sequence_reset(style, model_list):
"Returns a list of the SQL statements to reset sequences for the given models."
from django.db import models
output = []
for model in model_list:
for f in model._meta.fields:
if isinstance(f, models.AutoField):
output.append("%s setval('%s', (%s max(%s) %s %s));" % \
(style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD('%s_%s_seq' % (model._meta.db_table, f.column)),
style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD(quote_name(f.column)),
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));" % \
(style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD('%s_id_seq' % f.m2m_db_table()),
style.SQL_KEYWORD('SELECT'),
style.SQL_FIELD(quote_name('id')),
style.SQL_KEYWORD('FROM'),
style.SQL_TABLE(f.m2m_db_table())))
return output

OPERATOR_MAPPING = {
'exact': '= %s',
Expand Down
5 changes: 5 additions & 0 deletions django/db/backends/sqlite3/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ def get_sql_flush(style, tables, sequences):
# get_sql_flush() implementations). Just return SQL at this point
return sql

def get_sql_sequence_reset(style, model_list):
"Returns a list of the SQL statements to reset sequences for the given models."
# No sequence reset required
return []

def _sqlite_date_trunc(lookup_type, dt):
try:
dt = util.typecast_timestamp(dt)
Expand Down
2 changes: 1 addition & 1 deletion docs/django-admin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given appnames.
sqlsequencereset [appname appname ...]
----------------------------------------------

Prints the SQL statements for resetting PostgreSQL sequences for the given
Prints the SQL statements for resetting sequences for the given
appnames.

See http://simon.incutio.com/archive/2004/04/21/postgres for more information.
Expand Down
Empty file.
10 changes: 10 additions & 0 deletions tests/regressiontests/fixtures_regress/fixtures/sequence.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"pk": "1",
"model": "fixtures_regress.animal",
"fields": {
"name": "Lion",
"latin_name": "Panthera leo"
}
}
]
22 changes: 22 additions & 0 deletions tests/regressiontests/fixtures_regress/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.db import models

class Animal(models.Model):
name = models.CharField(maxlength=150)
latin_name = models.CharField(maxlength=150)

def __str__(self):
return self.common_name

__test__ = {'API_TESTS':"""
>>> from django.core import management
# Load a fixture that uses PK=1
>>> management.load_data(['sequence'], verbosity=0)
# Create a new animal. Without a sequence reset, this new object
# will take a PK of 1 (on Postgres), and the save will fail.
# This is a regression test for ticket #3790.
>>> animal = Animal(name='Platypus', latin_name='Ornithorhynchus anatinus')
>>> animal.save()
"""}

0 comments on commit dabd966

Please sign in to comment.