﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35539	SearchVector GinIndex raises IMMUTABLE error	Alastair D'Silva		"I have a Document model with the following subclass:
{{{
    class Meta:
        indexes = [
            GinIndex(SearchVector('search_vector'), name='Document search vector'),  # Create a GIN index on the search vector
        ]
}}}

It creates the following migration:
{{{
        migrations.AddIndex(
            model_name='document',
            index=django.contrib.postgres.indexes.GinIndex(django.contrib.postgres.search.SearchVector('search_vector'), name='Document search vector'),
        ),
}}}

which in turn attempts to create the following index:
{{{
CREATE INDEX ""Document search vector"" ON ""documents_document"" USING gin ((to_tsvector(COALESCE((""search_vector"")::text, ''))))
}}}

This fails with: 
{{{
ERROR:  functions in index expression must be marked IMMUTABLE
}}}

If I update contrib/postgres/search.py as follows:
91:
{{{
function = """"
}}}

and 116-128:
{{{
#        clone.set_source_expressions(
#            [
#               Coalesce(
#                    (
#                        expression
#                        if isinstance(expression.output_field, (CharField, TextField))
#                        else Cast(expression, TextField())
#                    ),
#                    Value(""""),
#                )
#                for expression in clone.get_source_expressions()
#            ]
#        )
}}}

I can successfully create the index, and searches using SearchRank successfully use it."	Bug	closed	contrib.postgres	5.0	Normal	invalid		Mariusz Felisiak Simon Charette	Accepted	0	0	0	0	0	0
