2012-11-11 71 views
3

我从Django开始。我有一些网站设置与SQLite工作,但更改DB引擎postgresql后manage.py syncdb返回错误。我一直在谷歌搜索2天,但仍然没有为我工作.Postgres用户'乔'有超级用户权限和本地'乔'数据库存在。Django with postgresql - manage.py syncdb返回错误

运行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 

谢谢!

+0

问题出在应用程序 - 它是开源项目,我下载了一个新版本,这个问题不再发生。谢谢。 – Michal

回答

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

0

首先,您的数据库是否已正确同步?尝试运行python manage.py syncdb。如果这没有帮助,请继续阅读...

这是postgres在查询产生错误时执行的操作,并且您在尝试运行另一个查询时没有首先回滚事务以防错误。所以当出现问题时,您应该先DB数据库状态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() 

希望这会有所帮助。

0

在我的情况下,我不得不关闭一些应用程序,做执行syncdb,再次启用应用程序并再次syncdb。

相关问题