2015-07-13 73 views
6

当我试图(通过postgresql-contrib包)来安装unaccent Postgres的扩展,一切正常按以下:怎样才能激活已经存在的模型unaccent扩展

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

然而,当我尝试与Django 1.8一起使用,我得到以下错误:

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. 

使用Postgresql 9.3和Django 1.8。

回答

11

迁移文件需要手动制作和应用。

首先,创建一个空迁移:

./manage.py makemigrations myapp --empty 

然后打开文件,并添加UnaccentExtensionoperations

from django.contrib.postgres.operations import UnaccentExtension 


class Migration(migrations.Migration): 

    dependencies = [ 
     (<snip>) 
    ] 

    operations = [ 
     UnaccentExtension() 
    ] 

现在申请使用./manage.py migrate迁移。

如果你会得到在这最后一步以下错误:

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

...然后暂时允许超级用户权限的用户通过执行postgres# ALTER ROLE <user_name> SUPERUSER;及其NOSUPERUSER对应。 pgAdminIII也可以做到这一点。

现在使用Django享受unaccent功能:

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