我運行的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