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

发布于 2024-09-06 13:27:19 字数 4641 浏览 4 评论 0原文

我在从转储数据安装固定装置时遇到奇怪的错误。我正在使用 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(值, 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 中 返回查询.execute_sql(return_id) 文件“/opt/local/lib/python2.5/site-packages/django/db/models/sql/subqueries.py”,第320行,在execute_sql中 游标 = super(InsertQuery, self).execute_sql(无) 文件“/opt/local/lib/python2.5/site-packages/django/db/models/sql/query.py”,第2369行,在execute_sql中 光标.执行(sql,参数) 文件“/opt/local/lib/python2.5/site-packages/django/db/backends/util.py”,第 19 行,执行中 返回 self.cursor.execute(sql, params) 编程错误:无法适应

I'm having strange error with installing fixture from dumped data. I am using psycopg2, and 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

First I've checked similar issues on internet. This one seemed to be very related: http://code.djangoproject.com/ticket/5996, as my data has many non ASCII symbols

But actually I've checked my django installation and it's ok there

Could you advice what is wrong

====

Continued investigation after added print statement as suggested by the first answer. Log looks this way:

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$ 

Error from very last comment

<DeserializedObject: qwert2000's profile>

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 154, 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖兔^ω^ 2024-09-13 13:27:19

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

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

为什么!cursor.execute()会引发异常无法适应

Psycopg 通过查找将 Python 对象转换为 SQL 字符串表示形式
在对象类中。当您尝试通过时会引发异常
作为查询参数,没有为其注册适配器的对象
它的阶级。有关信息,请参阅:ref:adapting-new-types

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

好吧,我希望loaddata 的详细信息是可行的,而且我不必承认我从未找到一种使用 django 的 loaddata 调试适应错误的优雅方法。过去,我曾在 django 的 loaddata 函数中插入打印语句,以便在发生错误时可以看到正在反序列化的值。我已经编辑了django/core/management/loaddata.py。查看 handle() 函数中的 obj.save()。我希望这个坦白能够激励有人分享更好的解决方案:-)

The can't adapt error is raised by psycopg2 when it receives an data type that it doesn't know how to translate into a value for a SQL statement. For example, if you accidentally pass a list, say, for a value that is supposed to be an integer, psycopg2 will raise this can't adapt error.

The faq.txt document that ships with the source distribution of psycopg2 explains it this way:

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.

Probably your best first-pass at finding the offending value is to run loaddata in fully verbose mode: python manage.py loaddata --verbosity=2 /Users/oleg/probs.json

Well, I was hoping loaddata verbosity would work and I wouldn't have to confess that I've never found an elegant way of debugging adaptation errors with django's loaddata. In the past, I've resorted to inserting print statements in django's loaddata function so that I can see the values being deserialized when the error occurs. I've edited django/core/management/loaddata.py. Look of obj.save() in the handle() function. I hope this confession inspires someone to share a better solution :-)

桃扇骨 2024-09-13 13:27:19

好吧,我结束了从数据库复制转储,并在没有 python 的情况下在本地恢复它......

Ok, I ended copying dump from my database, and restoring it locally without python...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文