summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVlastimil Zíma <vlastimil.zima@nic.cz>2014-05-19 18:19:35 +0200
committerTim Graham <timograham@gmail.com>2014-06-20 11:59:02 -0400
commit78c32f1caa9cb9b28be2d867aff586a2016122ed (patch)
tree3a8449118905a66cad8324e1747523348a90835c
parent9a46836a0c3e15021df242f3dd5a33306f7e27bc (diff)
Fixed #22514 -- Prevented indexes on virtual fields [postgres].
-rw-r--r--django/db/backends/postgresql_psycopg2/creation.py4
-rw-r--r--tests/indexes/models.py25
-rw-r--r--tests/indexes/tests.py7
3 files changed, 34 insertions, 2 deletions
diff --git a/django/db/backends/postgresql_psycopg2/creation.py b/django/db/backends/postgresql_psycopg2/creation.py
index 2594ba1d5f..d363faaa82 100644
--- a/django/db/backends/postgresql_psycopg2/creation.py
+++ b/django/db/backends/postgresql_psycopg2/creation.py
@@ -47,7 +47,8 @@ class DatabaseCreation(BaseDatabaseCreation):
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
@@ -73,7 +74,6 @@ class DatabaseCreation(BaseDatabaseCreation):
# 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'))
diff --git a/tests/indexes/models.py b/tests/indexes/models.py
index 064a099b40..c3d43913dd 100644
--- a/tests/indexes/models.py
+++ b/tests/indexes/models.py
@@ -2,10 +2,35 @@ from django.db import connection
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"],
diff --git a/tests/indexes/tests.py b/tests/indexes/tests.py
index 82b69ab192..786c6b5cb4 100644
--- a/tests/indexes/tests.py
+++ b/tests/indexes/tests.py
@@ -29,3 +29,10 @@ class IndexesTests(TestCase):
# 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)