Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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.

Fixes https://code.djangoproject.com/ticket/12728
  • Loading branch information...
commit 214a97ac27f539345a82e63ee1c79cbeadcad3ed 1 parent 94e91f7
@lorin lorin authored
Showing with 4 additions and 3 deletions.
  1. +4 −3 django/db/backends/postgresql_psycopg2/operations.py
View
7 django/db/backends/postgresql_psycopg2/operations.py
@@ -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_KEYWORD('SELECT'),
style.SQL_TABLE(qn(f.m2m_db_table())),
- 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)),
style.SQL_KEYWORD('IS NOT'),
style.SQL_KEYWORD('FROM'),
style.SQL_TABLE(qn(f.m2m_db_table()))))
Please sign in to comment.
Something went wrong with that request. Please try again.