Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3790 -- Fixed a problem with sequence resetting during fixture…

… 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...
commit dabd96646cf04f0b4dc346f90baf8199e225f128 1 parent e339a41
Russell Keith-Magee authored April 06, 2007
1  AUTHORS
@@ -192,6 +192,7 @@ answer newbie questions, and generally made Django that much better:
192 192
     Ville Säävuori <http://www.unessa.net/>
193 193
     Tyson Tate <tyson@fallingbullets.com>
194 194
     thebjorn <bp@datakortet.no>
  195
+    Zach Thompson <zthompson47@gmail.com>
195 196
     Tom Tobin
196 197
     Joe Topjian <http://joe.terrarum.net/geek/code/python/django/>
197 198
     torne-django@wolfpuppy.org.uk
38  django/core/management.py
@@ -410,30 +410,10 @@ def get_sql_initial_data(apps):
410 410
 get_sql_initial_data.args = ''
411 411
 
412 412
 def get_sql_sequence_reset(app):
413  
-    "Returns a list of the SQL statements to reset PostgreSQL sequences for the given app."
  413
+    "Returns a list of the SQL statements to reset sequences for the given app."
414 414
     from django.db import backend, models
415  
-    output = []
416  
-    for model in models.get_models(app):
417  
-        for f in model._meta.fields:
418  
-            if isinstance(f, models.AutoField):
419  
-                output.append("%s setval('%s', (%s max(%s) %s %s));" % \
420  
-                    (style.SQL_KEYWORD('SELECT'),
421  
-                    style.SQL_FIELD('%s_%s_seq' % (model._meta.db_table, f.column)),
422  
-                    style.SQL_KEYWORD('SELECT'),
423  
-                    style.SQL_FIELD(backend.quote_name(f.column)),
424  
-                    style.SQL_KEYWORD('FROM'),
425  
-                    style.SQL_TABLE(backend.quote_name(model._meta.db_table))))
426  
-                break # Only one AutoField is allowed per model, so don't bother continuing.
427  
-        for f in model._meta.many_to_many:
428  
-            output.append("%s setval('%s', (%s max(%s) %s %s));" % \
429  
-                (style.SQL_KEYWORD('SELECT'),
430  
-                style.SQL_FIELD('%s_id_seq' % f.m2m_db_table()),
431  
-                style.SQL_KEYWORD('SELECT'),
432  
-                style.SQL_FIELD(backend.quote_name('id')),
433  
-                style.SQL_KEYWORD('FROM'),
434  
-                style.SQL_TABLE(f.m2m_db_table())))
435  
-    return output
436  
-get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting PostgreSQL sequences for the given app name(s)."
  415
+    return backend.get_sql_sequence_reset(style, models.get_models(app))
  416
+get_sql_sequence_reset.help_doc = "Prints the SQL statements for resetting sequences for the given app name(s)."
437 417
 get_sql_sequence_reset.args = APP_ARGS
438 418
 
439 419
 def get_sql_indexes(app):
@@ -1333,13 +1313,14 @@ def load_data(fixture_labels, verbosity=1):
1333 1313
     "Installs the provided fixture file(s) as data in the database."
1334 1314
     from django.db.models import get_apps
1335 1315
     from django.core import serializers
1336  
-    from django.db import connection, transaction
  1316
+    from django.db import connection, transaction, backend
1337 1317
     from django.conf import settings
1338 1318
     import sys
1339 1319
 
1340 1320
     # Keep a count of the installed objects and fixtures
1341 1321
     count = [0,0]
1342  
-
  1322
+    models = set()
  1323
+    
1343 1324
     humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path'
1344 1325
 
1345 1326
     # Get a cursor (even though we don't need one yet). This has
@@ -1401,6 +1382,7 @@ def load_data(fixture_labels, verbosity=1):
1401 1382
                             objects =  serializers.deserialize(format, fixture)
1402 1383
                             for obj in objects:
1403 1384
                                 count[0] += 1
  1385
+                                models.add(obj.object.__class__)
1404 1386
                                 obj.save()
1405 1387
                             label_found = True
1406 1388
                         except Exception, e:
@@ -1422,6 +1404,12 @@ def load_data(fixture_labels, verbosity=1):
1422 1404
     else:
1423 1405
         if verbosity > 0:
1424 1406
             print "Installed %d object(s) from %d fixture(s)" % tuple(count)
  1407
+        sequence_sql = backend.get_sql_sequence_reset(style, models)
  1408
+        if sequence_sql:
  1409
+            if verbosity > 1:
  1410
+                print "Resetting sequences"
  1411
+            for line in sequence_sql:
  1412
+                cursor.execute(line)
1425 1413
     transaction.commit()
1426 1414
     transaction.leave_transaction_management()
1427 1415
 
5  django/db/backends/ado_mssql/base.py
@@ -151,6 +151,11 @@ def get_sql_flush(sql_styler, full_table_list):
151 151
                  sql_styler.SQL_FIELD(quote_name(table))
152 152
                  )  for table in full_table_list]
153 153
 
  154
+def get_sql_sequence_reset(style, model_list):
  155
+    "Returns a list of the SQL statements to reset sequences for the given models."
  156
+    # No sequence reset required
  157
+    return []
  158
+
154 159
 OPERATOR_MAPPING = {
155 160
     'exact': '= %s',
156 161
     'iexact': 'LIKE %s',
1  django/db/backends/dummy/base.py
@@ -40,5 +40,6 @@ def close(self):
40 40
 get_fulltext_search_sql = complain
41 41
 get_drop_foreignkey_sql = complain
42 42
 get_sql_flush = complain
  43
+get_sql_sequence_reset = complain
43 44
 
44 45
 OPERATOR_MAPPING = {}
5  django/db/backends/mysql/base.py
@@ -215,6 +215,11 @@ def get_sql_flush(style, tables, sequences):
215 215
     else:
216 216
         return []
217 217
 
  218
+def get_sql_sequence_reset(style, model_list):
  219
+    "Returns a list of the SQL statements to reset sequences for the given models."
  220
+    # No sequence reset required
  221
+    return []
  222
+
218 223
 OPERATOR_MAPPING = {
219 224
     'exact': '= %s',
220 225
     'iexact': 'LIKE %s',
5  django/db/backends/mysql_old/base.py
@@ -217,6 +217,11 @@ def get_sql_flush(style, tables, sequences):
217 217
     else:
218 218
         return []
219 219
 
  220
+def get_sql_sequence_reset(style, model_list):
  221
+    "Returns a list of the SQL statements to reset sequences for the given models."
  222
+    # No sequence reset required
  223
+    return []
  224
+
220 225
 OPERATOR_MAPPING = {
221 226
     'exact': '= %s',
222 227
     'iexact': 'LIKE %s',
4  django/db/backends/oracle/base.py
@@ -134,6 +134,10 @@ def get_sql_flush(style, tables, sequences):
134 134
              style.SQL_FIELD(quote_name(table))
135 135
              )  for table in tables]
136 136
 
  137
+def get_sql_sequence_reset(style, model_list):
  138
+    "Returns a list of the SQL statements to reset sequences for the given models."
  139
+    # No sequence reset required
  140
+    return []
137 141
 
138 142
 OPERATOR_MAPPING = {
139 143
     'exact': '= %s',
24  django/db/backends/postgresql/base.py
@@ -213,6 +213,30 @@ def get_sql_flush(style, tables, sequences):
213 213
     else:
214 214
         return []
215 215
 
  216
+def get_sql_sequence_reset(style, model_list):
  217
+    "Returns a list of the SQL statements to reset sequences for the given models."
  218
+    from django.db import models
  219
+    output = []
  220
+    for model in model_list:
  221
+        for f in model._meta.fields:
  222
+            if isinstance(f, models.AutoField):
  223
+                output.append("%s setval('%s', (%s max(%s) %s %s));" % \
  224
+                    (style.SQL_KEYWORD('SELECT'),
  225
+                    style.SQL_FIELD('%s_%s_seq' % (model._meta.db_table, f.column)),
  226
+                    style.SQL_KEYWORD('SELECT'),
  227
+                    style.SQL_FIELD(quote_name(f.column)),
  228
+                    style.SQL_KEYWORD('FROM'),
  229
+                    style.SQL_TABLE(quote_name(model._meta.db_table))))
  230
+                break # Only one AutoField is allowed per model, so don't bother continuing.
  231
+        for f in model._meta.many_to_many:
  232
+            output.append("%s setval('%s', (%s max(%s) %s %s));" % \
  233
+                (style.SQL_KEYWORD('SELECT'),
  234
+                style.SQL_FIELD('%s_id_seq' % f.m2m_db_table()),
  235
+                style.SQL_KEYWORD('SELECT'),
  236
+                style.SQL_FIELD(quote_name('id')),
  237
+                style.SQL_KEYWORD('FROM'),
  238
+                style.SQL_TABLE(f.m2m_db_table())))
  239
+    return output
216 240
         
217 241
 # Register these custom typecasts, because Django expects dates/times to be
218 242
 # in Python's native (standard-library) datetime/time format, whereas psycopg
25  django/db/backends/postgresql_psycopg2/base.py
@@ -169,6 +169,31 @@ def get_sql_flush(style, tables, sequences):
169 169
         return sql
170 170
     else:
171 171
         return []
  172
+
  173
+def get_sql_sequence_reset(style, model_list):
  174
+    "Returns a list of the SQL statements to reset sequences for the given models."
  175
+    from django.db import models
  176
+    output = []
  177
+    for model in model_list:
  178
+        for f in model._meta.fields:
  179
+            if isinstance(f, models.AutoField):
  180
+                output.append("%s setval('%s', (%s max(%s) %s %s));" % \
  181
+                    (style.SQL_KEYWORD('SELECT'),
  182
+                    style.SQL_FIELD('%s_%s_seq' % (model._meta.db_table, f.column)),
  183
+                    style.SQL_KEYWORD('SELECT'),
  184
+                    style.SQL_FIELD(quote_name(f.column)),
  185
+                    style.SQL_KEYWORD('FROM'),
  186
+                    style.SQL_TABLE(quote_name(model._meta.db_table))))
  187
+                break # Only one AutoField is allowed per model, so don't bother continuing.
  188
+        for f in model._meta.many_to_many:
  189
+            output.append("%s setval('%s', (%s max(%s) %s %s));" % \
  190
+                (style.SQL_KEYWORD('SELECT'),
  191
+                style.SQL_FIELD('%s_id_seq' % f.m2m_db_table()),
  192
+                style.SQL_KEYWORD('SELECT'),
  193
+                style.SQL_FIELD(quote_name('id')),
  194
+                style.SQL_KEYWORD('FROM'),
  195
+                style.SQL_TABLE(f.m2m_db_table())))
  196
+    return output
172 197
         
173 198
 OPERATOR_MAPPING = {
174 199
     'exact': '= %s',
5  django/db/backends/sqlite3/base.py
@@ -170,6 +170,11 @@ def get_sql_flush(style, tables, sequences):
170 170
     # get_sql_flush() implementations). Just return SQL at this point
171 171
     return sql
172 172
 
  173
+def get_sql_sequence_reset(style, model_list):
  174
+    "Returns a list of the SQL statements to reset sequences for the given models."
  175
+    # No sequence reset required
  176
+    return []
  177
+    
173 178
 def _sqlite_date_trunc(lookup_type, dt):
174 179
     try:
175 180
         dt = util.typecast_timestamp(dt)
2  docs/django-admin.txt
@@ -366,7 +366,7 @@ Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given appnames.
366 366
 sqlsequencereset [appname appname ...]
367 367
 ----------------------------------------------
368 368
 
369  
-Prints the SQL statements for resetting PostgreSQL sequences for the given
  369
+Prints the SQL statements for resetting sequences for the given
370 370
 appnames.
371 371
 
372 372
 See http://simon.incutio.com/archive/2004/04/21/postgres for more information.
0  tests/regressiontests/fixtures_regress/__init__.py
No changes.
10  tests/regressiontests/fixtures_regress/fixtures/sequence.json
... ...
@@ -0,0 +1,10 @@
  1
+[
  2
+    {
  3
+        "pk": "1", 
  4
+        "model": "fixtures_regress.animal", 
  5
+        "fields": {
  6
+            "name": "Lion", 
  7
+            "latin_name": "Panthera leo"
  8
+        }
  9
+    }
  10
+]
22  tests/regressiontests/fixtures_regress/models.py
... ...
@@ -0,0 +1,22 @@
  1
+from django.db import models
  2
+
  3
+class Animal(models.Model):
  4
+    name = models.CharField(maxlength=150)
  5
+    latin_name = models.CharField(maxlength=150)
  6
+
  7
+    def __str__(self):
  8
+        return self.common_name    
  9
+
  10
+__test__ = {'API_TESTS':"""
  11
+>>> from django.core import management
  12
+
  13
+# Load a fixture that uses PK=1
  14
+>>> management.load_data(['sequence'], verbosity=0)
  15
+        
  16
+# Create a new animal. Without a sequence reset, this new object
  17
+# will take a PK of 1 (on Postgres), and the save will fail.
  18
+# This is a regression test for ticket #3790.
  19
+>>> animal = Animal(name='Platypus', latin_name='Ornithorhynchus anatinus')
  20
+>>> animal.save()
  21
+
  22
+"""}

0 notes on commit dabd966

Please sign in to comment.
Something went wrong with that request. Please try again.