2011-10-05 43 views
22

我有一個具有PointField屬性的地理模型。一切都完美的作品在當地,但是當我嘗試保存在服務器上的一個實例,我得到以下錯誤:保存geodjango PointField時出錯

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

我挖到源,發現價值被序列化不同;特別是,在服務器上執行查詢之前,該值不會被轉義。它看起來好像正在通過psycopg2.Binary.getquoted()完成轉義,並且確實無法在服務器上返回正確的值。

在我的機器:

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

在服務器上:

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

好吧,這可以解釋爲什麼它認爲我試圖插入一個空字節。 (因爲我是。)所以,現在我已經足夠了解Jonathan S.在django-users group上找到類似的報告有什麼問題,但是像Jonathan一樣,我不知道這是錯誤還是配置錯誤。

有人可以指出我正確的方向嗎?

以下是關於設置的一些信息:

  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 

回答

42

終於弄明白。

this ticket所述,差異在於默認情況下Postgres 9.1具有standard_conforming_strings。這真的不是問題,除非Django的適配器有一個基本上忽略它的錯誤。 A patch was submitted它在爲我工作。

對於那些不願意或無法應用補丁或升級的用戶,只需使用this database adapter instead即可。

+1

感謝分享matthew。我真的很高興,我偶然發現你的帖子:) – infiniteloop

+0

很高興它幫助! – matthewwithanm

+0

謝謝!在postgresql.conf中設置standard_conforming_strings = off也適用於我。 – Pablo