Error Saving geodjango PointField

▼魔方 西西 提交于 2019-12-03 06:30:51

问题


I have a geo model with a PointField property. Everything works perfectly locally, but when I try to save an instance on the server, I get the following error:

django.db.utils.DatabaseError: invalid byte sequence for encoding "UTF8": 0x00

I dug into the source and found that the values are being serialized differently; specifically, that value isn't being escaped before the query is executed on the server. It looks like the escaping is being done by psycopg2.Binary.getquoted() and sure enough, it doesn't return the correct value on the server.

On my machine:

from psycopg2 import Binary
Binary('\0').getquoted() # > "'\\\\000'::bytea"

On the server:

from psycopg2 import Binary
Binary('\0').getquoted() # > "'\\000'::bytea"

Okay, that explains why it thinks I'm trying to insert a null byte. (Because I am.) So now I know enough about what's going wrong to find a similar report by Jonathan S. on the django-users group but, like Jonathan, I don't know if this is a bug or configuration error.

Can somebody point me in the right direction?

Here's some info about the setups:

          My computer      Server
OS        OSX 10.7         CentOS 5.5
Python    2.7              2.6
Django    1.3              1.3
Postgres  9.0.4            9.9.1
postgis   1.5.2            1.5.3-2.rhel5
geos      3.3.0            3.3.0-1.rhel5

回答1:


Finally managed to figure it out.

The difference, as documented in this ticket, is that Postgres 9.1 has standard_conforming_strings on by default. Which wouldn't be a problem, really, except Django's adapter has a bug that basically ignores it. A patch was submitted and it's working for me.

For those unwilling or unable to apply the patch or upgrade, you can just use this database adapter instead.



来源:https://stackoverflow.com/questions/7667724/error-saving-geodjango-pointfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!