django:从 postgres 数据库导入数据时无法适应错误

标签 django postgresql psycopg2

我在从转储数据安装夹具时遇到奇怪的错误。我正在使用 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, raw=True) 文件“/opt/local/lib/python2.5/site-packages/django/db/models/base.py”,第 495 行,在 save_base 结果 = manager._insert(values, return_id=update_pk) 文件“/opt/local/lib/python2.5/site-packages/django/db/models/manager.py”,第 177 行,在 _insert 返回 insert_query(self.model, values, **kwargs) 文件“/opt/local/lib/python2.5/site-packages/django/db/models/query.py”,第 1087 行,in insert_query 返回查询.execute_sql(return_id) execute_sql 中的文件“/opt/local/lib/python2.5/site-packages/django/db/models/sql/subqueries.py”,第 320 行 cursor = super(InsertQuery, self).execute_sql(无) 文件“/opt/local/lib/python2.5/site-packages/django/db/models/sql/query.py”,第 2369 行,在 execute_sql cursor.execute(sql, 参数) 执行文件“/opt/local/lib/python2.5/site-packages/django/db/backends/util.py”,第 19 行 返回 self.cursor.execute(sql, params) ProgrammingError: 无法适应

最佳答案

can't adapt 错误在 psycopg2 接收到不知道如何转换为 SQL 语句的值的数据类型时引发。例如,如果您不小心传递了一个列表,例如,一个应该是整数的值,psycopg2 将引发此无法适应错误。

psycopg2 源代码分发附带的 faq.txt 文档是这样解释的:

Why does !cursor.execute() raise the exception can't adapt?

Psycopg converts Python objects in a SQL string representation by looking at the object class. The exception is raised when you are trying to pass as query parameter an object for which there is no adapter registered for its class. See :ref:adapting-new-types for informations.

找到违规值的最佳第一步可能是在完全详细模式下运行 loaddata:python manage.py loaddata --verbosity=2/Users/oleg/probs.json

好吧,我希望 loaddata 的冗长性能够起作用,而且我不必承认我从来没有找到一种优雅的方法来调试 django 的 loaddata 的适配错误。过去,我求助于在 django 的 loaddata 函数中插入打印语句,以便在发生错误时可以看到反序列化的值。我编辑了 django/core/management/loaddata.py。查看 handle() 函数中的 obj.save()。我希望这个忏悔能激发人们分享更好的解决方案:-)

关于django:从 postgres 数据库导入数据时无法适应错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3054837/

相关文章:

python - 如何获取模板中任意给定级别的深度级别

django - 如何使用 django-postman 为每个用户提供发送消息的能力

PostgreSQL 将非递归项的输出转换为正确的类型

sql - 下限 LIMIT/高 OFFSET 的成本非常高

ruby-on-rails - Cloud9 "fe_sendauth: no password supplied"中的 postgresql 错误

Python 日期 - 将 None 转换为 Null

python-3.x - 类型错误 : Object of type 'DataFrame' is not JSON serializable

python - 无论如何让 Django CMS 页面只能访问(站点端)只有少数人?

python - 将列名列表传递给 psycopg2 中的查询

javascript - Django调试工具栏错误路径