我运行的Django 1.9,Postgres的9.5.1在Mac OS XDjango的测试 - 无法删除并重新测试数据库
当我运行/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.
所以,按照This post,我跑:
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' - 默认情况下它工作,并且比userlocation这个数据库被检测为现有的。现有会话仍然是当前运行测试的django使用的会话。而脚本死后没有会话了:) – dahrens
看看那些[文档](https://docs.djangoproject.com/en/1.10/topics/testing/advanced/#controlling-creation-order-for -test-databases) – dahrens
@dahrens,谢谢。修正了一些设置文件,并像魅力一样工作。 – fiacre