开发者

Error Saving geodjango PointField

开发者 https://www.devze.com 2023-04-10 18:54 出处:网络
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:

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 f开发者_Go百科ound 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消