Skip to content

Commit 21a690f

Browse files
committed
Fixed #8901 -- Corrected the PostgreSQL sequence reset code when field identifiers exceed the maximum identifier length. Thanks to [email protected] for the report, and Matt Hoskins for the patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13328 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 9ebf411 commit 21a690f

File tree

5 files changed

+91
-15
lines changed

5 files changed

+91
-15
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ answer newbie questions, and generally made Django that much better:
220220
Kieran Holland <http://www.kieranholland.com>
221221
Sung-Jin Hong <[email protected]>
222222
Leo "hylje" Honkanen <[email protected]>
223+
Matt Hoskins <[email protected]>
223224
Tareque Hossain <http://www.codexn.com>
224225
Richard House <[email protected]>
225226
Robert Rock Howard <http://djangomojo.com/>

django/db/backends/postgresql/creation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.db.backends.creation import BaseDatabaseCreation
2+
from django.db.backends.util import truncate_name
23

34
class DatabaseCreation(BaseDatabaseCreation):
45
# This dictionary maps Field objects to their associated PostgreSQL column
@@ -51,7 +52,7 @@ def sql_indexes_for_field(self, model, f, style):
5152

5253
def get_index_sql(index_name, opclass=''):
5354
return (style.SQL_KEYWORD('CREATE INDEX') + ' ' +
54-
style.SQL_TABLE(qn(index_name)) + ' ' +
55+
style.SQL_TABLE(qn(truncate_name(index_name,self.connection.ops.max_name_length()))) + ' ' +
5556
style.SQL_KEYWORD('ON') + ' ' +
5657
style.SQL_TABLE(qn(db_table)) + ' ' +
5758
"(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) +

django/db/backends/postgresql/operations.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ def field_cast_sql(self, db_type):
5454
return '%s'
5555

5656
def last_insert_id(self, cursor, table_name, pk_name):
57-
cursor.execute("SELECT CURRVAL('\"%s_%s_seq\"')" % (table_name, pk_name))
57+
# Use pg_get_serial_sequence to get the underlying sequence name
58+
# from the table name and column name (available since PostgreSQL 8)
59+
cursor.execute("SELECT CURRVAL(pg_get_serial_sequence('%s','%s'))" % (table_name, pk_name))
5860
return cursor.fetchone()[0]
5961

6062
def no_limit_value(self):
@@ -90,13 +92,14 @@ def sql_flush(self, style, tables, sequences):
9092
for sequence_info in sequences:
9193
table_name = sequence_info['table']
9294
column_name = sequence_info['column']
93-
if column_name and len(column_name) > 0:
94-
sequence_name = '%s_%s_seq' % (table_name, column_name)
95-
else:
96-
sequence_name = '%s_id_seq' % table_name
97-
sql.append("%s setval('%s', 1, false);" % \
95+
if not (column_name and len(column_name) > 0):
96+
# This will be the case if it's an m2m using an autogenerated
97+
# intermediate table (see BaseDatabaseIntrospection.sequence_list)
98+
column_name = 'id'
99+
sql.append("%s setval(pg_get_serial_sequence('%s','%s'), 1, false);" % \
98100
(style.SQL_KEYWORD('SELECT'),
99-
style.SQL_FIELD(self.quote_name(sequence_name)))
101+
style.SQL_TABLE(table_name),
102+
style.SQL_FIELD(column_name))
100103
)
101104
return sql
102105
else:
@@ -110,11 +113,15 @@ def sequence_reset_sql(self, style, model_list):
110113
# Use `coalesce` to set the sequence for each model to the max pk value if there are records,
111114
# or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true
112115
# if there are records (as the max pk value is already in use), otherwise set it to false.
116+
# Use pg_get_serial_sequence to get the underlying sequence name from the table name
117+
# and column name (available since PostgreSQL 8)
118+
113119
for f in model._meta.local_fields:
114120
if isinstance(f, models.AutoField):
115-
output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
121+
output.append("%s setval(pg_get_serial_sequence('%s','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
116122
(style.SQL_KEYWORD('SELECT'),
117-
style.SQL_FIELD(qn('%s_%s_seq' % (model._meta.db_table, f.column))),
123+
style.SQL_TABLE(model._meta.db_table),
124+
style.SQL_FIELD(f.column),
118125
style.SQL_FIELD(qn(f.column)),
119126
style.SQL_FIELD(qn(f.column)),
120127
style.SQL_KEYWORD('IS NOT'),
@@ -123,9 +130,10 @@ def sequence_reset_sql(self, style, model_list):
123130
break # Only one AutoField is allowed per model, so don't bother continuing.
124131
for f in model._meta.many_to_many:
125132
if not f.rel.through:
126-
output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
133+
output.append("%s setval(pg_get_serial_sequence('%s','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
127134
(style.SQL_KEYWORD('SELECT'),
128-
style.SQL_FIELD(qn('%s_id_seq' % f.m2m_db_table())),
135+
style.SQL_TABLE(model._meta.db_table),
136+
style.SQL_FIELD('id'),
129137
style.SQL_FIELD(qn('id')),
130138
style.SQL_FIELD(qn('id')),
131139
style.SQL_KEYWORD('IS NOT'),

tests/regressiontests/backends/models.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from django.conf import settings
12
from django.db import models
2-
from django.db import connection
3+
from django.db import connection, DEFAULT_DB_ALIAS
4+
35

46
class Square(models.Model):
57
root = models.IntegerField()
@@ -8,18 +10,33 @@ class Square(models.Model):
810
def __unicode__(self):
911
return "%s ** 2 == %s" % (self.root, self.square)
1012

13+
1114
class Person(models.Model):
1215
first_name = models.CharField(max_length=20)
1316
last_name = models.CharField(max_length=20)
1417

1518
def __unicode__(self):
1619
return u'%s %s' % (self.first_name, self.last_name)
1720

21+
1822
class SchoolClass(models.Model):
1923
year = models.PositiveIntegerField()
2024
day = models.CharField(max_length=9, blank=True)
2125
last_updated = models.DateTimeField()
2226

27+
# Unfortunately, the following model breaks MySQL hard.
28+
# Until #13711 is fixed, this test can't be run under MySQL.
29+
if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':
30+
class VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ(models.Model):
31+
class Meta:
32+
# We need to use a short actual table name or
33+
# we hit issue #8548 which we're not testing!
34+
verbose_name = 'model_with_long_table_name'
35+
primary_key_is_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.AutoField(primary_key=True)
36+
charfield_is_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.CharField(max_length=100)
37+
m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.ManyToManyField(Person,blank=True)
38+
39+
2340
qn = connection.ops.quote_name
2441

2542
__test__ = {'API_TESTS': """

tests/regressiontests/backends/tests.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# -*- coding: utf-8 -*-
22
# Unit and doctests for specific database backends.
33
import datetime
4-
import models
54
import unittest
5+
6+
from django.conf import settings
7+
from django.core import management
8+
from django.core.management.color import no_style
69
from django.db import backend, connection, DEFAULT_DB_ALIAS
710
from django.db.backends.signals import connection_created
8-
from django.conf import settings
911
from django.test import TestCase
1012

13+
from regressiontests.backends import models
14+
1115
class Callproc(unittest.TestCase):
1216

1317
def test_dbms_session(self):
@@ -76,6 +80,7 @@ def test_django_extract(self):
7680
classes = models.SchoolClass.objects.filter(last_updated__day=20)
7781
self.assertEqual(len(classes), 1)
7882

83+
7984
class ParameterHandlingTest(TestCase):
8085
def test_bad_parameter_count(self):
8186
"An executemany call with too many/not enough parameters will raise an exception (Refs #12612)"
@@ -88,6 +93,50 @@ def test_bad_parameter_count(self):
8893
self.assertRaises(Exception, cursor.executemany, query, [(1,2,3),])
8994
self.assertRaises(Exception, cursor.executemany, query, [(1,),])
9095

96+
# Unfortunately, the following tests would be a good test to run on all
97+
# backends, but it breaks MySQL hard. Until #13711 is fixed, it can't be run
98+
# everywhere (although it would be an effective test of #13711).
99+
if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.mysql':
100+
class LongNameTest(TestCase):
101+
"""Long primary keys and model names can result in a sequence name
102+
that exceeds the database limits, which will result in truncation
103+
on certain databases (e.g., Postgres). The backend needs to use
104+
the correct sequence name in last_insert_id and other places, so
105+
check it is. Refs #8901.
106+
"""
107+
108+
def test_sequence_name_length_limits_create(self):
109+
"""Test creation of model with long name and long pk name doesn't error. Ref #8901"""
110+
models.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.objects.create()
111+
112+
def test_sequence_name_length_limits_m2m(self):
113+
"""Test an m2m save of a model with a long name and a long m2m field name doesn't error as on Django >=1.2 this now uses object saves. Ref #8901"""
114+
obj = models.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ.objects.create()
115+
rel_obj = models.Person.objects.create(first_name='Django', last_name='Reinhardt')
116+
obj.m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.add(rel_obj)
117+
118+
def test_sequence_name_length_limits_flush(self):
119+
"""Test that sequence resetting as part of a flush with model with long name and long pk name doesn't error. Ref #8901"""
120+
# A full flush is expensive to the full test, so we dig into the
121+
# internals to generate the likely offending SQL and run it manually
122+
123+
# Some convenience aliases
124+
VLM = models.VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
125+
VLM_m2m = VLM.m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz.through
126+
tables = [
127+
VLM._meta.db_table,
128+
VLM_m2m._meta.db_table,
129+
]
130+
sequences = [
131+
{
132+
'column': VLM._meta.pk.column,
133+
'table': VLM._meta.db_table
134+
},
135+
]
136+
cursor = connection.cursor()
137+
for statement in connection.ops.sql_flush(no_style(), tables, sequences):
138+
cursor.execute(statement)
139+
91140

92141
def connection_created_test(sender, **kwargs):
93142
print 'connection_created signal'

0 commit comments

Comments
 (0)