我正在 Mac OS X 上运行 Django 1.9、Postgres 9.5.1。
当我运行 /manage.py test --settings=myproj.settings.local
时,会得到以下结果:
Creating test database for alias 'default'...
Creating test database for alias 'userlocation'...
Got an error creating the test database: database "test_myproj" already exists
Type 'yes' if you would like to try deleting the test database 'test_myproj', or 'no' to cancel: yes
Destroying old test database for alias 'userlocation'...
Got an error recreating the test database: database "test_myproj" is being accessed by other users
DETAIL: There is 1 other session using the database.
所以,根据这篇文章,我运行:
SELECT
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
pid <> pg_backend_pid()
AND datname = 'test_myproj'
;
尝试运行测试并进行DROP DATABASE test_myproj
,结果出现错误DETAIL: There is 1 other session using the database.
查看进程列表后,没有任何与数据库相关的内容。我杀掉服务器并重新启动,但运行测试的管理命令仍然报告有另一个会话连接到数据库。
这真是个难题,我以前从未遇到过这种情况--有其他人遇到过吗?
我的设置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproj',
'USER': 'myuser',
'PASSWORD': 'mypass',
'HOST': 'localhost',
'PORT': '',
},
'userlocation': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'og_myproj',
'USER': 'og_myuser',
'PASSWORD': 'mypasswd',
'HOST': 'localhost',
'PORT': '5432',
}
}
test_myproj
- 对于默认情况下它可以工作,然后对于用户位置,这个数据库被检测为已存在。现有的会话是当前运行测试的Django仍在使用的会话。当脚本死亡后就没有会话了 ;) - dahrens