Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #11107 -- Corrected the generation of sequence reset SQL for m2…

…m fields with an intermediate model. Thanks to J Clifford Dyer for the report and fix.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@11215 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 52bc78262172ad6e8deb3c75bd65edea68924dbc 1 parent 55968a6
Russell Keith-Magee authored July 11, 2009
1  AUTHORS
@@ -131,6 +131,7 @@ answer newbie questions, and generally made Django that much better:
131 131
     Andrew Durdin <adurdin@gmail.com>
132 132
     dusk@woofle.net
133 133
     Andy Dustman <farcepest@gmail.com>
  134
+    J. Clifford Dyer <jcd@unc.edu>
134 135
     Clint Ecker
135 136
     Nick Efford <nick@efford.org>
136 137
     eibaan@gmail.com
13  django/db/backends/oracle/base.py
@@ -217,12 +217,13 @@ def sequence_reset_sql(self, style, model_list):
217 217
                     # continue to loop
218 218
                     break
219 219
             for f in model._meta.many_to_many:
220  
-                table_name = self.quote_name(f.m2m_db_table())
221  
-                sequence_name = get_sequence_name(f.m2m_db_table())
222  
-                column_name = self.quote_name('id')
223  
-                output.append(query % {'sequence': sequence_name,
224  
-                                       'table': table_name,
225  
-                                       'column': column_name})
  220
+                if not f.rel.through:
  221
+                    table_name = self.quote_name(f.m2m_db_table())
  222
+                    sequence_name = get_sequence_name(f.m2m_db_table())
  223
+                    column_name = self.quote_name('id')
  224
+                    output.append(query % {'sequence': sequence_name,
  225
+                                           'table': table_name,
  226
+                                           'column': column_name})
226 227
         return output
227 228
 
228 229
     def start_transaction_sql(self):
17  django/db/backends/postgresql/operations.py
@@ -121,14 +121,15 @@ def sequence_reset_sql(self, style, model_list):
121 121
                         style.SQL_TABLE(qn(model._meta.db_table))))
122 122
                     break # Only one AutoField is allowed per model, so don't bother continuing.
123 123
             for f in model._meta.many_to_many:
124  
-                output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
125  
-                    (style.SQL_KEYWORD('SELECT'),
126  
-                    style.SQL_FIELD(qn('%s_id_seq' % f.m2m_db_table())),
127  
-                    style.SQL_FIELD(qn('id')),
128  
-                    style.SQL_FIELD(qn('id')),
129  
-                    style.SQL_KEYWORD('IS NOT'),
130  
-                    style.SQL_KEYWORD('FROM'),
131  
-                    style.SQL_TABLE(qn(f.m2m_db_table()))))
  124
+                if not f.rel.through:
  125
+                    output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
  126
+                        (style.SQL_KEYWORD('SELECT'),
  127
+                        style.SQL_FIELD(qn('%s_id_seq' % f.m2m_db_table())),
  128
+                        style.SQL_FIELD(qn('id')),
  129
+                        style.SQL_FIELD(qn('id')),
  130
+                        style.SQL_KEYWORD('IS NOT'),
  131
+                        style.SQL_KEYWORD('FROM'),
  132
+                        style.SQL_TABLE(qn(f.m2m_db_table()))))
132 133
         return output
133 134
 
134 135
     def savepoint_create_sql(self, sid):
34  tests/regressiontests/m2m_through_regress/fixtures/m2m_through.json
... ...
@@ -0,0 +1,34 @@
  1
+[
  2
+    {
  3
+        "pk": "1",
  4
+        "model": "m2m_through_regress.person",
  5
+        "fields": {
  6
+            "name": "Guido"
  7
+        }
  8
+    },
  9
+    {
  10
+        "pk": "1",
  11
+        "model": "auth.user",
  12
+        "fields": {
  13
+             "username": "Guido",
  14
+             "email": "bdfl@python.org",
  15
+             "password": "abcde"
  16
+        }
  17
+    },
  18
+    {
  19
+        "pk": "1",
  20
+        "model": "m2m_through_regress.group",
  21
+        "fields": {
  22
+            "name": "Python Core Group"
  23
+        }
  24
+    },
  25
+    {
  26
+        "pk": "1",
  27
+        "model": "m2m_through_regress.usermembership",
  28
+        "fields": {
  29
+            "user": "1",
  30
+            "group": "1",
  31
+            "price": "100"
  32
+        }
  33
+    }
  34
+]
10  tests/regressiontests/m2m_through_regress/models.py
@@ -12,7 +12,9 @@ class Membership(models.Model):
12 12
     def __unicode__(self):
13 13
         return "%s is a member of %s" % (self.person.name, self.group.name)
14 14
 
  15
+# using custom id column to test ticket #11107
15 16
 class UserMembership(models.Model):
  17
+    id = models.AutoField(db_column='usermembership_id', primary_key=True)
16 18
     user = models.ForeignKey(User)
17 19
     group = models.ForeignKey('Group')
18 20
     price = models.IntegerField(default=100)
@@ -196,4 +198,12 @@ class B(models.Model):
196 198
 # Flush the database, just to make sure we can.
197 199
 >>> management.call_command('flush', verbosity=0, interactive=False)
198 200
 
  201
+## Regression test for #11107
  202
+Ensure that sequences on m2m_through tables are being created for the through
  203
+model, not for a phantom auto-generated m2m table.
  204
+
  205
+>>> management.call_command('loaddata', 'm2m_through', verbosity=0)
  206
+>>> management.call_command('dumpdata', 'm2m_through_regress', format='json')
  207
+[{"pk": 1, "model": "m2m_through_regress.usermembership", "fields": {"price": 100, "group": 1, "user": 1}}, {"pk": 1, "model": "m2m_through_regress.person", "fields": {"name": "Guido"}}, {"pk": 1, "model": "m2m_through_regress.group", "fields": {"name": "Python Core Group"}}]
  208
+
199 209
 """}

0 notes on commit 52bc782

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