Django的:从Postgres数据库导入数据时无法适应错误
-
27-09-2019 - |
题
我在使用从转储的数据安装夹具奇怪的错误。我使用psycopg2,和django1.1.1
silver:probsbox oleg$ python manage.py loaddata /Users/oleg/probs.json
Installing json fixture '/Users/oleg/probs' from '/Users/oleg/probs'.
Problem installing fixture '/Users/oleg/probs.json': Traceback (most recent call last):
File "/opt/local/lib/python2.5/site-packages/django/core/management/commands/loaddata.py", line 153, in handle
obj.save()
File "/opt/local/lib/python2.5/site-packages/django/core/serializers/base.py", line 163, in save
models.Model.save_base(self.object, raw=True)
File "/opt/local/lib/python2.5/site-packages/django/db/models/base.py", line 495, in save_base
result = manager._insert(values, return_id=update_pk)
File "/opt/local/lib/python2.5/site-packages/django/db/models/manager.py", line 177, in _insert
return insert_query(self.model, values, **kwargs)
File "/opt/local/lib/python2.5/site-packages/django/db/models/query.py", line 1087, in insert_query
return query.execute_sql(return_id)
File "/opt/local/lib/python2.5/site-packages/django/db/models/sql/subqueries.py", line 320, in execute_sql
cursor = super(InsertQuery, self).execute_sql(None)
File "/opt/local/lib/python2.5/site-packages/django/db/models/sql/query.py", line 2369, in execute_sql
cursor.execute(sql, params)
File "/opt/local/lib/python2.5/site-packages/django/db/backends/util.py", line 19, in execute
return self.cursor.execute(sql, params)
ProgrammingError: can't adapt
首先我检查互联网上类似的问题。这一个似乎非常有关: http://code.djangoproject.com/ticket/5996,作为我的数据具有许多非ASCII符号
但实际上我已经检查了我的Django的安装和它的确定有
莫非你的建议是什么错误
====
由第一个答案建议添加打印语句后继续调查。日志看起来是这样的:
silver:probsbox oleg$ python manage.py loaddata /Users/oleg/probs.json
Installing json fixture '/Users/oleg/probs' from '/Users/oleg/probs'.
<DeserializedObject: Novice>
<DeserializedObject: Junior>
<DeserializedObject: Chess enthusiast>
<DeserializedObject: Experienced player >
<DeserializedObject: Smart player>
Problem installing fixture '/Users/oleg/probs.json': Traceback (most recent call last):
File "/opt/local/lib/python2.5/site-packages/django/core/management/commands/loaddata.py", line 153, in handle
print obj
File "/opt/local/lib/python2.5/site-packages/django/core/serializers/base.py", line 155, in __repr__
return "<DeserializedObject: %s>" % smart_str(self.object)
File "/opt/local/lib/python2.5/site-packages/django/utils/encoding.py", line 107, in smart_str
return str(s)
File "/opt/local/lib/python2.5/site-packages/django/db/models/base.py", line 335, in __str__
return force_unicode(self).encode('utf-8')
File "/opt/local/lib/python2.5/site-packages/django/utils/encoding.py", line 71, in force_unicode
s = unicode(s)
File "/Users/oleg/Sites/probsbox/registration/models.py", line 58, in __unicode__
return u"%s's profile" %(self.user.username)
File "/opt/local/lib/python2.5/site-packages/django/db/models/fields/related.py", line 257, in __get__
rel_obj = QuerySet(self.field.rel.to).get(**params)
File "/opt/local/lib/python2.5/site-packages/django/db/models/query.py", line 305, in get
% self.model._meta.object_name)
DoesNotExist: User matching query does not exist.
silver:probsbox oleg$
这最后评论错误
<DeserializedObject: qwert2000's profile>
问题安装夹具“/Users/oleg/probs.json”:回溯(最近最后调用): 文件“/opt/local/lib/python2.5/site-packages/django/core/management/commands/loaddata.py”,线路154,在手柄 obj.save() 文件“/opt/local/lib/python2.5/site-packages/django/core/serializers/base.py”,线路163,在保存 models.Model.save_base(self.object,生=真) 文件 “/opt/local/lib/python2.5/site-packages/django/db/models/base.py”,线路495,在save_base 导致= manager._insert(值,return_id = update_pk) 文件 “/opt/local/lib/python2.5/site-packages/django/db/models/manager.py”,线路177,在_insert 返回insert_query(self.model,价值观,** kwargs) 文件 “/opt/local/lib/python2.5/site-packages/django/db/models/query.py”,线1087,在insert_query 返回query.execute_sql(return_id) 文件 “/opt/local/lib/python2.5/site-packages/django/db/models/sql/subqueries.py”,线320,在execute_sql 光标=超级(InsertQuery,自我).execute_sql(无) 文件 “/opt/local/lib/python2.5/site-packages/django/db/models/sql/query.py”,线2369,在execute_sql cursor.execute(SQL,则params) 文件“/opt/local/lib/python2.5/site-packages/django/db/backends/util.py” 19行,在执行 返回self.cursor.execute(SQL,则params) ProgrammingError:无法适应
解决方案 2
好吧,我最终从我的数据库复制转储,并在本地恢复它没有巨蟒...
其他提示
在can't adapt
错误是由psycopg2当它接收到的数据类型,它不知道如何转化为SQL语句中的值上升。例如,如果你不小心通过列表,比如,对于被认为是一个整数的值,psycopg2将提高这无法适应错误。
在faq.txt
文件附带psycopg2的源分布这样解释:
为什么
!cursor.execute()
引发异常的无法适应的?Psycopg Python对象在SQL字符串表示通过查找转换 在的对象类。唯一的例外是当你试图通过上调 作为查询参数的对象,其没有用于注册适配器 它的类。参见:参考:
adapting-new-types
用于信息
<击>可能是你最好第一通在发现有问题的值是在充分详细模式运行loaddata:蟒manage.py loaddata --verbosity = 2 /Users/oleg/probs.json
好了,我希望loaddata冗长会工作,我也不会承认,我从来没有发现调试适应错误Django的loaddata的一种优雅的方式。在过去,我已经诉诸了Django的loaddata功能将打印报表,这样我可以看到,当发生错误的值被反序列化。我已经编辑django/core/management/loaddata.py
。在obj.save()
功能看handle()
的。我希望这表白激励别人共享一个更好的解决方案: - )