2015-07-13 10 views
6

Quando provo ad installare l'unaccent Postgres estensione (attraverso il pacchetto postgresql-contrib), tutto funziona come per il seguito:Come posso attivare l'estensione unaccent su un modello già esistente

# 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) 

Tuttavia, quando provo da utilizzare con Django 1.8, ottengo il seguente errore:

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. 

Utilizzo di Postgresql 9.3 e Django 1.8.

risposta

11

Un file di migrazione deve essere creato e applicato manualmente.

In primo luogo, creare una migrazione vuota:

./manage.py makemigrations myapp --empty 

quindi aprire il file e aggiungere UnaccentExtension-operations:

from django.contrib.postgres.operations import UnaccentExtension 


class Migration(migrations.Migration): 

    dependencies = [ 
     (<snip>) 
    ] 

    operations = [ 
     UnaccentExtension() 
    ] 

Ora applicare la migrazione utilizzando ./manage.py migrate.

Se si otterrebbe seguente errore durante quel ultimo passo:

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

... poi consentire temporaneamente i diritti di superutente per l'utente eseguendo postgres# ALTER ROLE <user_name> SUPERUSER; e il suo omologo NOSUPERUSER. pgAdminIII può fare anche questo.

ora godere della funzionalità unaccent Django:

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