def test_multiple_conditions(self): with connection.schema_editor() as editor: index = Index( name='recent_article_idx', fields=['pub_date', 'headline'], condition=( Q(pub_date__gt=datetime.datetime( year=2015, month=1, day=1, tzinfo=timezone.get_current_timezone(), )) & Q(headline__contains='China') ), ) sql = str(index.create_sql(Article, schema_editor=editor)) where = sql.find('WHERE') self.assertIn( 'WHERE (%s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name("pub_date")), sql ) # Because each backend has different syntax for the operators, # check ONLY the occurrence of headline in the SQL. self.assertGreater(sql.rfind('headline'), where) editor.add_index(index=index, model=Article) self.assertIn(index.name, connection.introspection.get_constraints( cursor=connection.cursor(), table_name=Article._meta.db_table, )) editor.remove_index(index=index, model=Article)
def test_condition_ignored(self): index = Index( name='test_condition_ignored', fields=['published'], condition=Q(published=True), ) with connection.schema_editor() as editor: # This would error if condition weren't ignored. editor.add_index(Article, index) self.assertNotIn( 'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), 'published'), str(index.create_sql(Article, editor)) )
def test_boolean_restriction_partial(self): with connection.schema_editor() as editor: index = Index( name='published_index', fields=['published'], condition=Q(published=True), ) self.assertIn( 'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name('published')), str(index.create_sql(Article, schema_editor=editor)) ) editor.add_index(index=index, model=Article) self.assertIn(index.name, connection.introspection.get_constraints( cursor=connection.cursor(), table_name=Article._meta.db_table, ))
def test_is_null_condition(self): with connection.schema_editor() as editor: index = Index( name='recent_article_idx', fields=['pub_date'], condition=Q(pub_date__isnull=False), ) self.assertIn( 'WHERE %s.%s IS NOT NULL' % (editor.quote_name(Article._meta.db_table), editor.quote_name("pub_date")), str(index.create_sql(Article, schema_editor=editor)) ) editor.add_index(index=index, model=Article) self.assertIn(index.name, connection.introspection.get_constraints( cursor=connection.cursor(), table_name=Article._meta.db_table, ))
def test_ops_class_partial_tablespace(self): indexname = 'test_ops_class_tblspace' index = Index( name=indexname, fields=['body'], opclasses=['text_pattern_ops'], condition=Q(headline__contains='China'), db_tablespace='pg_default', ) with connection.schema_editor() as editor: editor.add_index(IndexedArticle2, index) self.assertIn('TABLESPACE "pg_default" ', str(index.create_sql(IndexedArticle2, editor))) with editor.connection.cursor() as cursor: cursor.execute(self.get_opclass_query % indexname) self.assertCountEqual(cursor.fetchall(), [('text_pattern_ops', indexname)])
def test_integer_restriction_partial(self): with connection.schema_editor() as editor: index = Index( name='recent_article_idx', fields=['id'], condition=Q(pk__gt=1), ) self.assertIn( 'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name('id')), str(index.create_sql(Article, schema_editor=editor)) ) editor.add_index(index=index, model=Article) self.assertIn(index.name, connection.introspection.get_constraints( cursor=connection.cursor(), table_name=Article._meta.db_table, )) editor.remove_index(index=index, model=Article)
def test_partial_index(self): with connection.schema_editor() as editor: index = Index( name='recent_article_idx', fields=['pub_date'], condition=Q( pub_date__gt=datetime.datetime( year=2015, month=1, day=1, # PostgreSQL would otherwise complain about the lookup # being converted to a mutable function (by removing # the timezone in the cast) which is forbidden. tzinfo=timezone.get_current_timezone(), ), ) ) self.assertIn( 'WHERE %s.%s' % (editor.quote_name(Article._meta.db_table), editor.quote_name("pub_date")), str(index.create_sql(Article, schema_editor=editor)) ) editor.add_index(index=index, model=Article) self.assertIn(index.name, connection.introspection.get_constraints( cursor=connection.cursor(), table_name=Article._meta.db_table, ))
class Meta: indexes = [ Index(fields=["activity"]), Index(fields=["pl", "user"]) ]
from __future__ import unicode_literals from django.db.models import F, Q from django.db.models.options import Options try: # Django >= 1.7 from django import apps except ImportError: # Django < 1.7 apps = None try: # Django >= 1.11 from django.db.models import Index _test_index = Index(fields=['test']) except ImportError: Index = None _test_index = None _options = Options({}) #: Index names changed in Django 1.5, with the introduction of index_together. supports_index_together = hasattr(_options, 'index_together') #: Whether new-style Index classes are available. #: #: Django 1.11 introduced formal support for defining explicit indexes not #: bound to a field definition or as part of #: ``index_together``/``unique_together``. #:
class Meta: unique_together = ( "facility", "item", ) indexes = [Index(fields=("facility", "item",))]
class Meta: indexes = [Index(fields=['address', '-nonce']) ] # Index on address and nonce DESC unique_together = (('internal_tx', 'address'), ) verbose_name_plural = 'Safe statuses'
class Meta: indexes = [Index(fields=['-created'])] unique_together = (('recipe', 'user_list'), )
class Meta: indexes = [Index(fields=('name', ))] verbose_name = _('Color') verbose_name_plural = _('Colors')
class Meta: ordering = ('name', ) indexes = [Index(fields=('name', ))] verbose_name = _('Car brand') verbose_name_plural = _('Car brands')
class Meta: indexes = [ Index(fields=['-rating', 'datetime']), # default index is desc ]
class Meta: ordering = ("name", ) indexes = [Index(fields=("name", ))] verbose_name = _("Car brand") verbose_name_plural = _("Car brands")
class Meta: indexes = [Index(fields=['keyword'])] constraints = [ UniqueConstraint(fields=['keyword', 'recipe'], name='unique_keyword'), ]
class Meta: indexes = [ Index(fields=['movie']), Index(fields=['date']), ]
class Meta: verbose_name = _('Order') verbose_name_plural = _('Orders') indexes = [Index(fields=['status', ])]
class Meta: indexes = (Index(fields=("classifier", "-created_on")), Index(fields=("ticketer", "-created_on")))
class Meta: indexes = [Index(fields=['post', 'creator'])]
class Meta: indexes = [Index(fields=("name", ))] verbose_name = _("Color") verbose_name_plural = _("Colors")
class Meta: ordering = ['first_name', 'last_name'] indexes = [ Index(fields=['first_name', 'last_name']), ]