简体   繁体   English

Django与postgresql - manage.py syncdb返回错误

[英]Django with postgresql - manage.py syncdb returns errors

I'm starting with Django. 我是从Django开始的。 I had some site set up with SQLite working but after changing DB engine to postgresql manage.py syncdb returns errors.I've been googling for 2 days but still nothing works for me.Postgres user 'joe' has superuser rights and local 'joe' db exists. 我有一些网站设置与SQLite工作,但在更改数据库引擎后postgresql manage.py syncdb返回错误。我已谷歌搜索2天但仍然没有任何作用对我.Postgres用户'乔'拥有超级用户权限和本地'乔'db存在。

Postgresql is running: Postgresql正在运行:

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

Here's my part of settings.py 这是我的settings.py的一部分

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

And the errors: 而错误:

$ 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

Thanks! 谢谢!

My suggestion would be to go to django.db.backends.postgresql_psycopg2.base and insert a print query so that CursorWrapper looks like. 我的建议是转到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)

That will print out every query hitting the database, and hopefully allow you to identify the offending SQL query when trying to run python manage.py syncdb 这将打印出每个查询数据库的查询,并希望您在尝试运行python manage.py syncdb时识别有问题的SQL查询

First, is your DB properly synced? 首先,你的数据库是否正确同步? Try running python manage.py syncdb . 尝试运行python manage.py syncdb If this does not help, then read on... 如果这没有帮助,请继续阅读......

This is what postgres does when a query produces an error and you try to run another query without first rolling back the transaction in case of error. 这是postgres在查询产生错误时所执行的操作,并且您尝试运行另一个查询而不首先在发生错误时回滚事务。 So you should first rollback the DB state when something goes wrong. 所以你应该在出现问题时首先rollback数据库状态。 Since its transactional based, it causes problems. 由于它以事务为基础,因此会引发问题。 To fix it, you'll want to figure out where in the code that bad query is being executed. 要修复它,您需要弄清楚代码中执行错误查询的位置。

It might be helpful to use the log_statement option in your postgresql server for debugging. 使用postgresql服务器中的log_statement选项进行调试可能会有所帮助。

The below solution is what I came up when I had same error. 以下解决方案是我遇到同样错误时出现的问题。

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()

Hope this helps. 希望这可以帮助。

在我的情况下,我不得不禁用一些应用程序,执行syncdb,再次启用应用程序和syncdb。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM