diff --git a/django/db/backends/postgresql_psycopg2/creation.py b/django/db/backends/postgresql_psycopg2/creation.py
index d4260e0..6ed1c45 100644
|
a
|
b
|
class DatabaseCreation(BaseDatabaseCreation):
|
| 41 | 41 | |
| 42 | 42 | def sql_indexes_for_field(self, model, f, style): |
| 43 | 43 | output = [] |
| 44 | | if f.db_index or f.unique: |
| | 44 | db_type = f.db_type(connection=self.connection) |
| | 45 | if db_type is not None and (f.db_index or f.unique): |
| 45 | 46 | qn = self.connection.ops.quote_name |
| 46 | 47 | db_table = model._meta.db_table |
| 47 | 48 | tablespace = f.db_tablespace or model._meta.db_tablespace |
| … |
… |
class DatabaseCreation(BaseDatabaseCreation):
|
| 67 | 68 | # a second index that specifies their operator class, which is |
| 68 | 69 | # needed when performing correct LIKE queries outside the |
| 69 | 70 | # C locale. See #12234. |
| 70 | | db_type = f.db_type(connection=self.connection) |
| 71 | 71 | if db_type.startswith('varchar'): |
| 72 | 72 | output.append(get_index_sql('%s_%s_like' % (db_table, f.column), |
| 73 | 73 | ' varchar_pattern_ops')) |
diff --git a/tests/indexes/models.py b/tests/indexes/models.py
index e38eb00..bdc75c2 100644
|
a
|
b
|
from django.db import connection
|
| 2 | 2 | from django.db import models |
| 3 | 3 | |
| 4 | 4 | |
| | 5 | class CurrentTranslation(models.ForeignObject): |
| | 6 | """ |
| | 7 | Creates virtual relation to the translation with model cache enabled. |
| | 8 | """ |
| | 9 | # Avoid validation |
| | 10 | requires_unique_target = False |
| | 11 | |
| | 12 | def __init__(self, to, from_fields, to_fields, **kwargs): |
| | 13 | # Disable reverse relation |
| | 14 | kwargs['related_name'] = '+' |
| | 15 | # Set unique to enable model cache. |
| | 16 | kwargs['unique'] = True |
| | 17 | super(CurrentTranslation, self).__init__(to, from_fields, to_fields, **kwargs) |
| | 18 | |
| | 19 | |
| | 20 | class ArticleTranslation(models.Model): |
| | 21 | |
| | 22 | article = models.ForeignKey('indexes.Article') |
| | 23 | language = models.CharField(max_length=10, unique=True) |
| | 24 | content = models.TextField() |
| | 25 | |
| | 26 | |
| 5 | 27 | class Article(models.Model): |
| 6 | 28 | headline = models.CharField(max_length=100) |
| 7 | 29 | pub_date = models.DateTimeField() |
| 8 | 30 | |
| | 31 | # Add virtual relation to the ArticleTranslation model. |
| | 32 | translation = CurrentTranslation(ArticleTranslation, ['id'], ['article']) |
| | 33 | |
| 9 | 34 | class Meta: |
| 10 | 35 | index_together = [ |
| 11 | 36 | ["headline", "pub_date"], |
diff --git a/tests/indexes/tests.py b/tests/indexes/tests.py
index 672e35d..5bf7866 100644
|
a
|
b
|
class IndexesTests(TestCase):
|
| 25 | 25 | # unique=True and db_index=True should only create the varchar-specific |
| 26 | 26 | # index (#19441). |
| 27 | 27 | self.assertIn('("slug" varchar_pattern_ops)', index_sql[4]) |
| | 28 | |
| | 29 | @skipUnless(connections[DEFAULT_DB_ALIAS].vendor == 'postgresql', |
| | 30 | "This is a postgresql-specific issue") |
| | 31 | def test_postgresql_virtual_relation_indexes(self): |
| | 32 | """Test indexes are not created for related objects""" |
| | 33 | from .models import Article |
| | 34 | connection = connections[DEFAULT_DB_ALIAS] |
| | 35 | index_sql = connection.creation.sql_indexes_for_model(Article, no_style()) |
| | 36 | self.assertEqual(len(index_sql), 1) |