summaryrefslogtreecommitdiff
path: root/tests
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 19:01:49 -0400
commitef3ae3d1c94ac1d4ffaeeeec1ee544e91431e65b (patch)
tree29ac530dd9f9f0df4f9fda26de9121f0df5e0dba /tests
parentd4a3fd44f04fd06252ccfdd001772daa35c510d1 (diff)
[1.6.x] Fixed #22514 -- Prevented indexes on virtual fields [postgres].
Backport of 78c32f1caa from master
Diffstat (limited to 'tests')
-rw-r--r--tests/indexes/models.py25
-rw-r--r--tests/indexes/tests.py9
2 files changed, 33 insertions, 1 deletions
diff --git a/tests/indexes/models.py b/tests/indexes/models.py
index e38eb005db..bdc75c2db7 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 672e35d286..694696b342 100644
--- a/tests/indexes/tests.py
+++ b/tests/indexes/tests.py
@@ -1,5 +1,5 @@
from django.core.management.color import no_style
-from django.db import connections, DEFAULT_DB_ALIAS
+from django.db import connection, connections, DEFAULT_DB_ALIAS
from django.test import TestCase
from django.utils.unittest import skipUnless
@@ -25,3 +25,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)