Code

Ticket #19441: 19441-2.diff

File 19441-2.diff, 1.7 KB (added by claudep, 18 months ago)
Line 
1diff --git a/django/db/backends/postgresql_psycopg2/creation.py b/django/db/backends/postgresql_psycopg2/creation.py
2index 90304aa..88afd5f 100644
3--- a/django/db/backends/postgresql_psycopg2/creation.py
4+++ b/django/db/backends/postgresql_psycopg2/creation.py
5@@ -42,7 +42,7 @@ class DatabaseCreation(BaseDatabaseCreation):
6 
7     def sql_indexes_for_field(self, model, f, style):
8         output = []
9-        if f.db_index:
10+        if f.db_index or f.unique:
11             qn = self.connection.ops.quote_name
12             db_table = model._meta.db_table
13             tablespace = f.db_tablespace or model._meta.db_tablespace
14diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt
15index a2081e0..c6a23db 100644
16--- a/docs/ref/models/fields.txt
17+++ b/docs/ref/models/fields.txt
18@@ -272,6 +272,9 @@ field, a :exc:`django.db.IntegrityError` will be raised by the model's
19 This option is valid on all field types except :class:`ManyToManyField` and
20 :class:`FileField`.
21 
22+Note that when ``unique`` is ``True``, you don't need to specify
23+:attr:`~Field.db_index`, because ``unique`` implies the creation of an index.
24+
25 ``unique_for_date``
26 -------------------
27 
28diff --git a/tests/regressiontests/indexes/models.py b/tests/regressiontests/indexes/models.py
29index 4ab74d2..e38eb00 100644
30--- a/tests/regressiontests/indexes/models.py
31+++ b/tests/regressiontests/indexes/models.py
32@@ -17,4 +17,4 @@ if connection.vendor == 'postgresql':
33     class IndexedArticle(models.Model):
34         headline = models.CharField(max_length=100, db_index=True)
35         body = models.TextField(db_index=True)
36-        slug = models.CharField(max_length=40, unique=True, db_index=True)
37+        slug = models.CharField(max_length=40, unique=True)