Author: russellm
Date: 2010-07-29 21:42:36 -0500 (Thu, 29 Jul 2010)
New Revision: 13449

Modified:
   django/trunk/django/db/backends/postgresql/operations.py
   django/trunk/tests/regressiontests/backends/models.py
   django/trunk/tests/regressiontests/backends/tests.py
Log:
Fixed #13941 -- Corrected the way sequence names are reset under Postgres, 
especially when generic foreign keys are involved. Thanks to Ales Zoulek for 
the report and patch.

Modified: django/trunk/django/db/backends/postgresql/operations.py
===================================================================
--- django/trunk/django/db/backends/postgresql/operations.py    2010-07-25 
20:58:58 UTC (rev 13448)
+++ django/trunk/django/db/backends/postgresql/operations.py    2010-07-30 
02:42:36 UTC (rev 13449)
@@ -132,7 +132,7 @@
                 if not f.rel.through:
                     output.append("%s 
setval(pg_get_serial_sequence('%s','%s'), coalesce(max(%s), 1), max(%s) %s 
null) %s %s;" % \
                         (style.SQL_KEYWORD('SELECT'),
-                        style.SQL_TABLE(model._meta.db_table),
+                        style.SQL_TABLE(f.m2m_db_table()),
                         style.SQL_FIELD('id'),
                         style.SQL_FIELD(qn('id')),
                         style.SQL_FIELD(qn('id')),

Modified: django/trunk/tests/regressiontests/backends/models.py
===================================================================
--- django/trunk/tests/regressiontests/backends/models.py       2010-07-25 
20:58:58 UTC (rev 13448)
+++ django/trunk/tests/regressiontests/backends/models.py       2010-07-30 
02:42:36 UTC (rev 13449)
@@ -1,3 +1,5 @@
+from django.contrib.contenttypes import generic
+from django.contrib.contenttypes.models import ContentType
 from django.conf import settings
 from django.db import models
 from django.db import connection, DEFAULT_DB_ALIAS
@@ -37,6 +39,19 @@
         m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = 
models.ManyToManyField(Person,blank=True)
 
 
+class Tag(models.Model):
+    name = models.CharField(max_length=30)
+    content_type = models.ForeignKey(ContentType)
+    object_id = models.PositiveIntegerField()
+    content_object = generic.GenericForeignKey('content_type', 'object_id')
+
+
+class Post(models.Model):
+    name = models.CharField(max_length=30)
+    text = models.TextField()
+    tags = generic.GenericRelation('Tag')
+
+
 qn = connection.ops.quote_name
 
 __test__ = {'API_TESTS': """

Modified: django/trunk/tests/regressiontests/backends/tests.py
===================================================================
--- django/trunk/tests/regressiontests/backends/tests.py        2010-07-25 
20:58:58 UTC (rev 13448)
+++ django/trunk/tests/regressiontests/backends/tests.py        2010-07-30 
02:42:36 UTC (rev 13449)
@@ -6,7 +6,7 @@
 from django.conf import settings
 from django.core import management
 from django.core.management.color import no_style
-from django.db import backend, connection, DEFAULT_DB_ALIAS
+from django.db import backend, connection, connections, DEFAULT_DB_ALIAS
 from django.db.backends.signals import connection_created
 from django.test import TestCase
 
@@ -137,7 +137,24 @@
             for statement in connection.ops.sql_flush(no_style(), tables, 
sequences):
                 cursor.execute(statement)
 
+class SequenceResetTest(TestCase):
+    def test_generic_relation(self):
+        "Sequence names are correct when resetting generic relations (Ref 
#13941)"
+        # Create an object with a manually specified PK
+        models.Post.objects.create(id=10, name='1st post', text='hello world')
 
+        # Reset the sequences for the database
+        cursor = connection.cursor()
+        commands = 
connections[DEFAULT_DB_ALIAS].ops.sequence_reset_sql(no_style(), [models.Post])
+        for sql in commands:
+            cursor.execute(sql)
+
+        # If we create a new object now, it should have a PK greater
+        # than the PK we specified manually.
+        obj = models.Post.objects.create(name='New post', text='goodbye world')
+        self.assertTrue(obj.pk > 10)
+
+
 def connection_created_test(sender, **kwargs):
     print 'connection_created signal'
 

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to