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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With