Skip to content

Commit

Permalink
[1.7.x] Fixed #22514 -- Prevented indexes on virtual fields [postgres].
Browse files Browse the repository at this point in the history
Backport of 78c32f1 from master
  • Loading branch information
ziima authored and timgraham committed Jun 20, 2014
1 parent d9a83d5 commit 6e5a736
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions django/db/backends/postgresql_psycopg2/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def sql_table_creation_suffix(self):

def sql_indexes_for_field(self, model, f, style):
output = []
if f.db_index or f.unique:
db_type = f.db_type(connection=self.connection)
if db_type is not None and (f.db_index or f.unique):
qn = self.connection.ops.quote_name
db_table = model._meta.db_table
tablespace = f.db_tablespace or model._meta.db_tablespace
Expand All @@ -73,7 +74,6 @@ def get_index_sql(index_name, opclass=''):
# a second index that specifies their operator class, which is
# needed when performing correct LIKE queries outside the
# C locale. See #12234.
db_type = f.db_type(connection=self.connection)
if db_type.startswith('varchar'):
output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
' varchar_pattern_ops'))
Expand Down
25 changes: 25 additions & 0 deletions tests/indexes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,35 @@
from django.db import models


class CurrentTranslation(models.ForeignObject):
"""
Creates virtual relation to the translation with model cache enabled.
"""
# Avoid validation
requires_unique_target = False

def __init__(self, to, from_fields, to_fields, **kwargs):
# Disable reverse relation
kwargs['related_name'] = '+'
# Set unique to enable model cache.
kwargs['unique'] = True
super(CurrentTranslation, self).__init__(to, from_fields, to_fields, **kwargs)


class ArticleTranslation(models.Model):

article = models.ForeignKey('indexes.Article')
language = models.CharField(max_length=10, unique=True)
content = models.TextField()


class Article(models.Model):
headline = models.CharField(max_length=100)
pub_date = models.DateTimeField()

# Add virtual relation to the ArticleTranslation model.
translation = CurrentTranslation(ArticleTranslation, ['id'], ['article'])

class Meta:
index_together = [
["headline", "pub_date"],
Expand Down
7 changes: 7 additions & 0 deletions tests/indexes/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ def test_postgresql_text_indexes(self):
# unique=True and db_index=True should only create the varchar-specific
# index (#19441).
self.assertIn('("slug" varchar_pattern_ops)', index_sql[4])

@skipUnless(connection.vendor == 'postgresql',
"This is a postgresql-specific issue")
def test_postgresql_virtual_relation_indexes(self):
"""Test indexes are not created for related objects"""
index_sql = connection.creation.sql_indexes_for_model(Article, no_style())
self.assertEqual(len(index_sql), 1)

0 comments on commit 6e5a736

Please sign in to comment.