commit 645f5dda46705b4c403f2cab0ff634abf92d92d8
Author: Honza Kral <honza.kral@gmail.com>
Date:   Thu May 27 17:35:32 2010 +0200

    fixed #2495

diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
index 492ac1e..06df548 100644
--- a/django/db/backends/creation.py
+++ b/django/db/backends/creation.py
@@ -253,12 +253,22 @@ class BaseDatabaseCreation(object):
             output.extend(self.sql_indexes_for_field(model, f, style))
         return output
 
-    def sql_indexes_for_field(self, model, f, style):
-        "Return the CREATE INDEX SQL statements for a single model field"
+    def compile_sql_index_for_field(self, db_table, f, style, tablespace_sql):
+        "Compile CREATE INDEX SQL statements for sql_indexes_for_field"
         from django.db.backends.util import truncate_name
 
+        qn = self.connection.ops.quote_name
+        index_name = '%s_%s' % (db_table, self._digest(f.column))
+        return [style.SQL_KEYWORD('CREATE INDEX') + ' ' +
+                style.SQL_TABLE(qn(truncate_name(index_name, self.connection.ops.max_name_length()))) + ' ' +
+                style.SQL_KEYWORD('ON') + ' ' +
+                style.SQL_TABLE(qn(db_table)) + ' ' +
+                "(%s)" % style.SQL_FIELD(qn(f.column)) +
+                "%s;" % tablespace_sql]
+
+    def sql_indexes_for_field(self, model, f, style):
+        "Return the CREATE INDEX SQL statements for a single model field"
         if f.db_index and not f.unique:
-            qn = self.connection.ops.quote_name
             tablespace = f.db_tablespace or model._meta.db_tablespace
             if tablespace:
                 sql = self.connection.ops.tablespace_sql(tablespace)
@@ -268,13 +278,7 @@ class BaseDatabaseCreation(object):
                     tablespace_sql = ''
             else:
                 tablespace_sql = ''
-            i_name = '%s_%s' % (model._meta.db_table, self._digest(f.column))
-            output = [style.SQL_KEYWORD('CREATE INDEX') + ' ' +
-                style.SQL_TABLE(qn(truncate_name(i_name, self.connection.ops.max_name_length()))) + ' ' +
-                style.SQL_KEYWORD('ON') + ' ' +
-                style.SQL_TABLE(qn(model._meta.db_table)) + ' ' +
-                "(%s)" % style.SQL_FIELD(qn(f.column)) +
-                "%s;" % tablespace_sql]
+            output = self.compile_sql_index_for_field(model._meta.db_table, f, style, tablespace_sql)
         else:
             output = []
         return output
diff --git a/django/db/backends/mysql/creation.py b/django/db/backends/mysql/creation.py
index 8b026a9..a3ef8df 100644
--- a/django/db/backends/mysql/creation.py
+++ b/django/db/backends/mysql/creation.py
@@ -63,3 +63,29 @@ class DatabaseCreation(BaseDatabaseCreation):
                 field.rel.to._meta.db_table, field.rel.to._meta.pk.column)
             ]
         return table_output, deferred
+
+    def compile_sql_index_for_field(self, db_table, f, style, tablespace_sql):
+        """
+        Adds a  prefix length to the TextField which is currently represented
+        by MySQL's 'longtext' type -- which requires a prefix-length parameter
+        in order to install an index. 
+
+        Use largest possible value allowed - 255 characters.
+        """
+        from django.db.backends.util import truncate_name
+
+        qn = self.connection.ops.quote_name
+        index_name = '%s_%s' % (db_table, self._digest(f.column))
+
+        if f.db_type() == self.data_types['TextField']:
+            field_name = style.SQL_FIELD(qn(f.column)) + '(255)'
+        else:
+            field_name = style.SQL_FIELD(qn(f.column))
+
+        return [style.SQL_KEYWORD('CREATE INDEX') + ' ' +
+                style.SQL_TABLE(qn(truncate_name(index_name, self.connection.ops.max_name_length()))) + ' ' +
+                style.SQL_KEYWORD('ON') + ' ' +
+                style.SQL_TABLE(qn(db_table)) + ' ' +
+                "(%s)" % field_name +
+                "%s;" % tablespace_sql]
+
diff --git a/django/db/backends/postgresql/creation.py b/django/db/backends/postgresql/creation.py
index af26d0b..3db1506 100644
--- a/django/db/backends/postgresql/creation.py
+++ b/django/db/backends/postgresql/creation.py
@@ -35,41 +35,29 @@ class DatabaseCreation(BaseDatabaseCreation):
             return "WITH ENCODING '%s'" % self.connection.settings_dict['TEST_CHARSET']
         return ''
 
-    def sql_indexes_for_field(self, model, f, style):
-        if f.db_index and not f.unique:
-            qn = self.connection.ops.quote_name
-            db_table = model._meta.db_table
-            tablespace = f.db_tablespace or model._meta.db_tablespace
-            if tablespace:
-                sql = self.connection.ops.tablespace_sql(tablespace)
-                if sql:
-                    tablespace_sql = ' ' + sql
-                else:
-                    tablespace_sql = ''
-            else:
-                tablespace_sql = ''
+    def compile_sql_index_for_field(self, db_table, f, style, tablespace_sql):
+        def _get_index_sql(index_name, opclass=''):
+            from django.db.backends.util import truncate_name
 
-            def get_index_sql(index_name, opclass=''):
-                return (style.SQL_KEYWORD('CREATE INDEX') + ' ' +
-                        style.SQL_TABLE(qn(index_name)) + ' ' +
-                        style.SQL_KEYWORD('ON') + ' ' +
-                        style.SQL_TABLE(qn(db_table)) + ' ' +
-                        "(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) +
-                        "%s;" % tablespace_sql)
+            qn = self.connection.ops.quote_name
+            return (style.SQL_KEYWORD('CREATE INDEX') + ' ' +
+                style.SQL_TABLE(qn(truncate_name(index_name, self.connection.ops.max_name_length()))) + ' ' +
+                style.SQL_KEYWORD('ON') + ' ' +
+                style.SQL_TABLE(qn(db_table)) + ' ' +
+                "(%s%s)" % (style.SQL_FIELD(qn(f.column)), opclass) +
+                "%s;" % tablespace_sql)
 
-            output = [get_index_sql('%s_%s' % (db_table, f.column))]
+        output = [_get_index_sql('%s_%s' % (db_table, f.column))]
 
-            # Fields with database column types of `varchar` and `text` need
-            # a second index that specifies their operator class, which is
-            # needed when performing correct LIKE queries outside the
-            # C locale. See #12234.
-            db_type = f.db_type()
-            if db_type.startswith('varchar'):
-                output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
-                                            ' varchar_pattern_ops'))
-            elif db_type.startswith('text'):
-                output.append(get_index_sql('%s_%s_like' % (db_table, f.column),
-                                            ' text_pattern_ops'))
-        else:
-            output = []
+        # Fields with database column types of `varchar` and `text` need
+        # a second index that specifies their operator class, which is
+        # needed when performing correct LIKE queries outside the
+        # C locale. See #12234.
+        db_type = f.db_type()
+        if db_type.startswith('varchar'):
+            output.append(_get_index_sql('%s_%s_like' % (db_table, f.column),
+                                        ' varchar_pattern_ops'))
+        elif db_type.startswith('text'):
+            output.append(_get_index_sql('%s_%s_like' % (db_table, f.column),
+                                        ' text_pattern_ops'))
         return output
diff --git a/tests/regressiontests/bug2495/__init__.py b/tests/regressiontests/bug2495/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/regressiontests/bug2495/models.py b/tests/regressiontests/bug2495/models.py
new file mode 100644
index 0000000..91661b3
--- /dev/null
+++ b/tests/regressiontests/bug2495/models.py
@@ -0,0 +1,15 @@
+from unittest import TestCase
+
+from django.db import models, connection
+
+class IndexedTextModel(models.Model):
+    indexed_text = models.TextField(db_index=True)
+
+class TestTextFieldIndices(TestCase):
+    def test_indexed_text_has_an_index(self):
+        try:
+            indexes = connection.introspection.get_indexes(connection.cursor(), IndexedTextModel._meta.db_table)
+        except NotImplementedError:
+            indexes = {}
+        assert 'indexed_text' in indexes
+
