Skip to content
Browse files

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.

  • Loading branch information...
1 parent 94e91f7 commit 214a97ac27f539345a82e63ee1c79cbeadcad3ed @lorin lorin committed
Showing with 4 additions and 3 deletions.
  1. +4 −3 django/db/backends/postgresql_psycopg2/
7 django/db/backends/postgresql_psycopg2/
@@ -133,12 +133,13 @@ def sequence_reset_sql(self, style, model_list):
break # Only one AutoField is allowed per model, so don't bother continuing.
for f in model._meta.many_to_many:
if not f.rel.through:
+ pk_column = f.m2m_reverse_name()
output.append("%s setval(pg_get_serial_sequence('%s','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
- style.SQL_FIELD('id'),
- style.SQL_FIELD(qn('id')),
- style.SQL_FIELD(qn('id')),
+ style.SQL_FIELD(pk_column),
+ style.SQL_FIELD(qn(pk_column)),
+ style.SQL_FIELD(qn(pk_column)),

0 comments on commit 214a97a

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