我是通过以下步骤从Django 1.7迁移到1.8
执行第4步时,我收到以下错误。
Unhandled exception in thread started by <function wrapper at 0x7f4e473a8230>
Traceback (most recent call last):
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 223, in wrapper
fn(*args, **kwargs)
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 112, in inner_run
self.check_migrations()
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 164, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 19, in __init__
self.loader = MigrationLoader(self.connection)
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 47, in __init__
self.build_graph()
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 180, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
self.ensure_schema()
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 162, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 135, in _cursor
self.ensure_connection()
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
self.connect()
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
self.connect()
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 119, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 172, in get_new_connection
connection = Database.connect(**conn_params)
File "/home/lenovo/Envs/boilerplate/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: invalid connection option "autocommit"
错误似乎来自psycopg2模块并且与autocommit相关。在1.8文档的删除功能部分中,我找到了以下行。
the decorators and context managers autocommit, commit_on_success, and commit_manually, defined in django.db.transaction
我无法将此与我得到的错误联系起来。有人可以对此嗤之以鼻吗?
我发现了原因。下面是我的数据库连接配置。在那里有autocommit = True。在评论该行时,问题得到了解决。但我仍然想知道为什么我们不能给autocommit = True选项。
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': 'localhost',
'PORT': '5432',
'NAME': 'bp_django_auth',
'USER': 'postgres',
'PASSWORD': 'abcd1234',
'OPTIONS': {
"autocommit": True,
},
}
}
答案 0 :(得分:14)
Django 1.7 Databases docs中列出了以下内容:
在以前的Django版本中,可以通过在DATABASES中的数据库配置的OPTIONS部分设置自动提交密钥来启用数据库级自动提交。
自Django 1.6以来,默认情况下自动提交已开启。此配置将被忽略,可以安全删除。
根据1.8 Release Notes,此功能已被删除。
如果由于某种原因仍想保留设置,只需将其移出OPTIONS
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': 'localhost',
'PORT': '5432',
'NAME': 'bp_django_auth',
'USER': 'postgres',
'PASSWORD': 'abcd1234',
'AUTOCOMMIT': True,
}
}