How can I activate an unaccent extension on an existing model

When I try to install the unaccent Postgres extension (via the postgresql-contrib package), everything works as follows:

 # psql -U postgres -W -h localhost Password for user postgres: psql (9.3.9) SSL connection (cipher: DHE-RSA-AES256-GCM-SHA384, bits: 256) Type "help" for help. postgres=# CREATE EXTENSION unaccent; CREATE EXTENSION postgres=# SELECT unaccent('Hélène'); unaccent ---------- Helene (1 row) 

However, when I try to use Django 1.8, I get the following error:

 ProgrammingError: function unaccent(character varying) does not exist LINE 1: ...able" WHERE ("my_table"."live" = true AND UNACCENT("... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts. 

Using Postgresql 9.3 and Django 1.8.

+6
source share
1 answer

The migration file must be done manually and applied.

First create an empty migration:

 ./manage.py makemigrations myapp --empty 

Then open the file and add UnaccentExtension to operations :

 from django.contrib.postgres.operations import UnaccentExtension class Migration(migrations.Migration): dependencies = [ (<snip>) ] operations = [ UnaccentExtension() ] 

Now apply the migration using ./manage.py migrate .

If at this last stage you get the following error:

 django.db.utils.ProgrammingError: permission denied to create extension "unaccent" HINT: Must be superuser to create this extension. 

... then temporarily grant superuser rights to your user by doing postgres# ALTER ROLE <user_name> SUPERUSER; and his NOSUPERUSER . pgAdminIII can do this too.

Now enjoy impeccable functionality using Django:

 >>> Person.objects.filter(first_name__unaccent=u"Helène") [<Person: Michels Hélène>] 
+12
source

All Articles