Ticket #22514: postgres.patch

File postgres.patch, 3.3 KB (added by vzima, 13 months ago)
  • django/db/backends/postgresql_psycopg2/creation.py

    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): 
    4141
    4242    def sql_indexes_for_field(self, model, f, style):
    4343        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):
    4546            qn = self.connection.ops.quote_name
    4647            db_table = model._meta.db_table
    4748            tablespace = f.db_tablespace or model._meta.db_tablespace
    class DatabaseCreation(BaseDatabaseCreation): 
    6768            # a second index that specifies their operator class, which is
    6869            # needed when performing correct LIKE queries outside the
    6970            # C locale. See #12234.
    70             db_type = f.db_type(connection=self.connection)
    7171            if db_type.startswith('varchar'):
    7272                output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
    7373                                            ' varchar_pattern_ops'))
  • tests/indexes/models.py

    diff --git a/tests/indexes/models.py b/tests/indexes/models.py
    index e38eb00..bdc75c2 100644
    a b from django.db import connection 
    22from django.db import models
    33
    44
     5class 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
     20class 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
    527class Article(models.Model):
    628    headline = models.CharField(max_length=100)
    729    pub_date = models.DateTimeField()
    830
     31    # Add virtual relation to the ArticleTranslation model.
     32    translation = CurrentTranslation(ArticleTranslation, ['id'], ['article'])
     33
    934    class Meta:
    1035        index_together = [
    1136            ["headline", "pub_date"],
  • tests/indexes/tests.py

    diff --git a/tests/indexes/tests.py b/tests/indexes/tests.py
    index 672e35d..5bf7866 100644
    a b class IndexesTests(TestCase): 
    2525        # unique=True and db_index=True should only create the varchar-specific
    2626        # index (#19441).
    2727        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)
Back to Top