Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13941 -- Corrected the way sequence names are reset under Post…

…gres, especially when generic foreign keys are involved. Thanks to Ales Zoulek for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13449 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit bab9aab9a2cd82706e2a15044db40b9fc85afbc5 1 parent cd8758e
Russell Keith-Magee authored July 30, 2010
2  django/db/backends/postgresql/operations.py
@@ -132,7 +132,7 @@ def sequence_reset_sql(self, style, model_list):
132 132
                 if not f.rel.through:
133 133
                     output.append("%s setval(pg_get_serial_sequence('%s','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
134 134
                         (style.SQL_KEYWORD('SELECT'),
135  
-                        style.SQL_TABLE(model._meta.db_table),
  135
+                        style.SQL_TABLE(f.m2m_db_table()),
136 136
                         style.SQL_FIELD('id'),
137 137
                         style.SQL_FIELD(qn('id')),
138 138
                         style.SQL_FIELD(qn('id')),
15  tests/regressiontests/backends/models.py
... ...
@@ -1,3 +1,5 @@
  1
+from django.contrib.contenttypes import generic
  2
+from django.contrib.contenttypes.models import ContentType
1 3
 from django.conf import settings
2 4
 from django.db import models
3 5
 from django.db import connection, DEFAULT_DB_ALIAS
@@ -37,6 +39,19 @@ class Meta:
37 39
         m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.ManyToManyField(Person,blank=True)
38 40
 
39 41
 
  42
+class Tag(models.Model):
  43
+    name = models.CharField(max_length=30)
  44
+    content_type = models.ForeignKey(ContentType)
  45
+    object_id = models.PositiveIntegerField()
  46
+    content_object = generic.GenericForeignKey('content_type', 'object_id')
  47
+
  48
+
  49
+class Post(models.Model):
  50
+    name = models.CharField(max_length=30)
  51
+    text = models.TextField()
  52
+    tags = generic.GenericRelation('Tag')
  53
+
  54
+
40 55
 qn = connection.ops.quote_name
41 56
 
42 57
 __test__ = {'API_TESTS': """
19  tests/regressiontests/backends/tests.py
@@ -6,7 +6,7 @@
6 6
 from django.conf import settings
7 7
 from django.core import management
8 8
 from django.core.management.color import no_style
9  
-from django.db import backend, connection, DEFAULT_DB_ALIAS
  9
+from django.db import backend, connection, connections, DEFAULT_DB_ALIAS
10 10
 from django.db.backends.signals import connection_created
11 11
 from django.test import TestCase
12 12
 
@@ -137,6 +137,23 @@ def test_sequence_name_length_limits_flush(self):
137 137
             for statement in connection.ops.sql_flush(no_style(), tables, sequences):
138 138
                 cursor.execute(statement)
139 139
 
  140
+class SequenceResetTest(TestCase):
  141
+    def test_generic_relation(self):
  142
+        "Sequence names are correct when resetting generic relations (Ref #13941)"
  143
+        # Create an object with a manually specified PK
  144
+        models.Post.objects.create(id=10, name='1st post', text='hello world')
  145
+
  146
+        # Reset the sequences for the database
  147
+        cursor = connection.cursor()
  148
+        commands = connections[DEFAULT_DB_ALIAS].ops.sequence_reset_sql(no_style(), [models.Post])
  149
+        for sql in commands:
  150
+            cursor.execute(sql)
  151
+
  152
+        # If we create a new object now, it should have a PK greater
  153
+        # than the PK we specified manually.
  154
+        obj = models.Post.objects.create(name='New post', text='goodbye world')
  155
+        self.assertTrue(obj.pk > 10)
  156
+
140 157
 
141 158
 def connection_created_test(sender, **kwargs):
142 159
     print 'connection_created signal'

0 notes on commit bab9aab

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