3

我从 Django 开始。我设置了一些使用 SQLite 工作的站点,但是在将 DB 引擎更改为 postgresql manage.py syncdb 后返回错误。我已经用谷歌搜索了 2 天,但对我仍然没有任何作用。Postgres 用户 'joe' 具有超级用户权限和本地 'joe ' 数据库存在。

Postgresql 正在运行:

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

这是我的部分 settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',  #
        'NAME': 'joe',                                       # 
        'USER': 'joe',                                       # 
        'PASSWORD': 'asdf',                                  # 
        'HOST': 'localhost',                                 # 
        'PORT': '5432',                       .
    }
}

和错误:

$ 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

谢谢!

4

3 回答 3

1

我的建议是去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)

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

于 2012-11-17T22:26:33.283 回答
0

首先,您的数据库是否正确同步?尝试运行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()

希望这可以帮助。

于 2012-11-11T19:12:26.900 回答
0

就我而言,我不得不禁用一些应用程序,执行同步数据库,再次启用应用程序并再次同步数据库。

于 2014-05-09T00:15:48.767 回答