From 012229914cb1391ee4fcb6c183a341a6491cd377 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Mon, 7 Jan 2013 17:54:30 +0100 Subject: [PATCH] [1.5.x] Created special PostgreSQL text indexes when unique is True Refs #19441. Backport of c698c55966 from master. --- django/db/backends/postgresql_psycopg2/creation.py | 2 +- docs/ref/models/fields.txt | 3 +++ tests/regressiontests/indexes/models.py | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/django/db/backends/postgresql_psycopg2/creation.py b/django/db/backends/postgresql_psycopg2/creation.py index 90304aa566..88afd5f52f 100644 --- a/django/db/backends/postgresql_psycopg2/creation.py +++ b/django/db/backends/postgresql_psycopg2/creation.py @@ -42,7 +42,7 @@ class DatabaseCreation(BaseDatabaseCreation): def sql_indexes_for_field(self, model, f, style): output = [] - if f.db_index: + if 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 diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index a2081e05f4..c6a23dbcb5 100644 --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -272,6 +272,9 @@ field, a :exc:`django.db.IntegrityError` will be raised by the model's This option is valid on all field types except :class:`ManyToManyField` and :class:`FileField`. +Note that when ``unique`` is ``True``, you don't need to specify +:attr:`~Field.db_index`, because ``unique`` implies the creation of an index. + ``unique_for_date`` ------------------- diff --git a/tests/regressiontests/indexes/models.py b/tests/regressiontests/indexes/models.py index 4ab74d25bd..e38eb005db 100644 --- a/tests/regressiontests/indexes/models.py +++ b/tests/regressiontests/indexes/models.py @@ -17,4 +17,4 @@ if connection.vendor == 'postgresql': class IndexedArticle(models.Model): headline = models.CharField(max_length=100, db_index=True) body = models.TextField(db_index=True) - slug = models.CharField(max_length=40, unique=True, db_index=True) + slug = models.CharField(max_length=40, unique=True)