我使用Django 1.7 ==它有4個應用:爲什麼遷移測試失敗,但不遷移?
frontend
game
geo
people
的應用程序的設置是這樣的:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'fandjango',
'people',
'geo',
'game',
'frontend'
)
和數據庫設置爲:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'hoods_raising',
'USER': 'hoods_raising',
'PASSWORD': 'hr$nestor$123',
'HOST': 'localhost',
'TEST_CHARSET': 'utf8mb4'
}
}
我的應用程序具有遷移和測試:
game
migrations
0001_initial.py
geo
migrations
0001_initial.py
tests.py
people
migrations
0001_initial.py
0002_install_data.py
個
許多文件被省略以縮小問題(如果需要的話,我會用更多文件擴大問題),例如models.py
,views.py
。
的0002_install_data.py
內容是:
class Migration(migrations.Migration):
dependencies = [
('people', '0001_initial'),
]
operations = [
migrations.RunPython(NamesInstaller(), lambda apps, schema_editor: None)
]
如果我跑manage.py migrate
安裝數據庫,一切正常。
如果我跑manage.py test
運行測試,第一步將是測試數據庫的安裝。發生了一些奇怪的事情:
要執行的第一個遷移是0002_install_data。其他表永遠不會創建(例如,授權表,地理位置表,遊戲表,fandjango表等),並且people
中的遷移0001_initial未運行。由於此類原因,在0002_install_data
(它表示0001_initial
不存在)中發生相關性錯誤。KeyError: u"Migration people.0002_install_data dependencies references nonexistent parent node (u'people', u'0001_initial')"
爲什麼會這樣發生?爲什麼test
命令不能正確安排應用程序遷移? (這不發生在manage.py migrate
)。
[文件](https://docs.djangoproject.com/en/1.8/topics/migrations/): '''一旦你壓扁遷移,則應提交它的旁邊遷移它會將此更改替換並分發給應用程序的所有正在運行的實例,確保它們運行遷移以將更改存儲在其數據庫中。 完成這一步之後,您必須將壓扁的遷移過渡到正常的初始遷移,通過......''' – Hussam 2016-04-07 09:52:15