使用postgresql的Django管理.pysyncdb返回错误

2024-09-28 18:51:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我从Django开始。我有一些网站设置与SQLite工作,但在把数据库引擎改为postgresql之后管理syncdb返回错误。我已经在google上搜索了2天,但是仍然没有任何结果我。博士后用户“joe”具有超级用户权限,并且存在本地“joe”db。在

Postgresql正在运行:

/etc/init.d/postgresql status
Running clusters: 9.1/main

这是我的一部分设置.py在

^{pr2}$

以及错误:

$ python manage.py syncdb
Syncing...
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "/home/m/.virtualenvs/mayan/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/home/m/.virtualenvs/mayan/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/m/.virtualenvs/mayan/local/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/m/.virtualenvs/mayan/local/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/home/m/.virtualenvs/mayan/local/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle
    return self.handle_noargs(**options)
  File "/home/m/.virtualenvs/mayan/local/lib/python2.7/site-packages/south/management/commands/syncdb.py", line 90, in handle_noargs
    syncdb.Command().execute(**options)
  File "/home/m/.virtualenvs/mayan/local/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/home/m/.virtualenvs/mayan/local/lib/python2.7/site-packages/django/core/management/base.py", line 351, in handle
    return self.handle_noargs(**options)
  File "/home/m/.virtualenvs/mayan/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 59, in handle_noargs
    tables = connection.introspection.table_names()
  File "/home/m/.virtualenvs/mayan/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 792, in table_names
    return self.get_table_list(cursor)
  File "/home/m/.virtualenvs/mayan/local/lib/python2.7/site-packages/django/db/backends/postgresql/introspection.py", line 31, in get_table_list
    AND pg_catalog.pg_table_is_visible(c.oid)""")
  File "/home/m/.virtualenvs/mayan/local/lib/python2.7/site-packages/django/db/backends/util.py", line 34, in execute
    return self.cursor.execute(sql, params)
  File "/home/m/.virtualenvs/mayan/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
    return self.cursor.execute(query, args)
django.db.utils.DatabaseError: current transaction is aborted, commands ignored until end of transaction block

谢谢!在


Tags: djangoinpyselfhomeexecutelibpackages
3条回答

首先,你的数据库是否正确同步?尝试运行python manage.py syncdb。如果这没用,那就继续读。。。在

当一个查询产生错误时,postgres就是这样做的,当您试图运行另一个查询而不首先回滚事务以防出现错误时。所以你应该先rollback 出错时的数据库状态。因为它是基于事务的,所以会产生问题。为了解决这个问题,您需要弄清楚在代码中哪里执行了错误的查询。在

在postgresql服务器中使用log_statement选项进行调试可能会有帮助。在

下面的解决方案是我在遇到同样错误时提出的解决方案。在

from django.db import transaction

@transaction.commit_manually
def db_insert():
    try:
        YourModel(name='hello world').save() #trying to save
    except: #any error rollback
        transaction.rollback()
    else: #if all fine, commit
        transaction.commit()

希望这有帮助。在

我的建议是转到django.db.backends.postgresql_psycopg2.base并插入一个print query,这样CursorWrapper看起来像。在

class CursorWrapper:
    ...
    def execute(self, query, args=None):
        print query # New print statement here
        try:
            return self.cursor.execute(query, args)

这将打印出每个访问数据库的查询,并希望允许您在尝试运行python manage.py syncdb时识别出有问题的SQL查询

在我的例子中,我不得不禁用一些应用程序,执行syncdb,再次启用apps和syncdb。在

相关问题 更多 >