2012-11-11 103 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。

相關問題