Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use proper primary key column for PostgreSQL when setting sequences

PostgreSQL was hard-coded to use the 'id' column when generating SQL
to set the sequence on the models of many-to-many relations. This
breaks when the relation uses table-based inheritence, where the
primary key will not be named 'id'.

This change uses the actual primary key column name.

Fixes https://code.djangoproject.com/ticket/12728
  • Loading branch information...
commit 214a97ac27f539345a82e63ee1c79cbeadcad3ed 1 parent 94e91f7
Lorin Hochstein authored July 30, 2012
7  django/db/backends/postgresql_psycopg2/operations.py
@@ -133,12 +133,13 @@ def sequence_reset_sql(self, style, model_list):
133 133
                     break # Only one AutoField is allowed per model, so don't bother continuing.
134 134
             for f in model._meta.many_to_many:
135 135
                 if not f.rel.through:
  136
+                    pk_column = f.m2m_reverse_name()
136 137
                     output.append("%s setval(pg_get_serial_sequence('%s','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
137 138
                         (style.SQL_KEYWORD('SELECT'),
138 139
                         style.SQL_TABLE(qn(f.m2m_db_table())),
139  
-                        style.SQL_FIELD('id'),
140  
-                        style.SQL_FIELD(qn('id')),
141  
-                        style.SQL_FIELD(qn('id')),
  140
+                        style.SQL_FIELD(pk_column),
  141
+                        style.SQL_FIELD(qn(pk_column)),
  142
+                        style.SQL_FIELD(qn(pk_column)),
142 143
                         style.SQL_KEYWORD('IS NOT'),
143 144
                         style.SQL_KEYWORD('FROM'),
144 145
                         style.SQL_TABLE(qn(f.m2m_db_table()))))

0 notes on commit 214a97a

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