我最近將Django升級到1.8,併爲新的開始建立了一個新的開發數據庫。遷移和依賴性進行得很順利,可以避免你得到的常見錯誤,並最終解決問題。該應用程序現在在本地工作就好了。Django 1.8測試問題:ProgrammingError:關係「auth_user」不存在
python manage.py test
這是我收到的錯誤:
但是,試圖運行測試時,我得到一個錯誤
django.db.utils.ProgrammingError: relation "auth_user" does not exist
不用說,Django的驗證模塊確實安裝在應用中遷移,所以我不確定發生了什麼。
這裏是萬一全堆棧跟蹤你需要它來偷看,但它簡化版,說什麼,甚至遠程對我有幫助找出這個錯誤的原因:
Traceback (most recent call last):
File "C:/Users/dabadaba/PycharmProjects/dogpatchsports_com/mysite/manage_sched_dev.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\__init__.py", line 354, in execute_from_command_line
utility.execute()
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\__init__.py", line 346, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\test.py", line 30, in run_from_argv
super(Command, self).run_from_argv(argv)
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\base.py", line 394, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\test.py", line 74, in execute
super(Command, self).execute(*args, **options)
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\base.py", line 445, in execute
output = self.handle(*args, **options)
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\test.py", line 90, in handle
failures = test_runner.run_tests(test_labels)
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\test\runner.py", line 210, in run_tests
old_config = self.setup_databases()
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\test\runner.py", line 166, in setup_databases
**kwargs
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\test\runner.py", line 370, in setup_databases
serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\db\backends\base\creation.py", line 368, in create_test_db
test_flush=not keepdb,
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\__init__.py", line 120, in call_command
return command.execute(*args, **defaults)
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\base.py", line 445, in execute
output = self.handle(*args, **options)
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\migrate.py", line 179, in handle
created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\core\management\commands\migrate.py", line 318, in sync_apps
cursor.execute(statement)
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\db\utils.py", line 98, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\dabadaba\Envs\django18\lib\site-packages\django\db\backends\utils.py", line 62, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "auth_user" does not exist
我可以找出一個解決方法this answer阻止測試任務運行migrate
,根據我一直在讀的應該是發生錯誤的地方。奇怪的是,因爲當我運行migrate
一切都很好。
但是,我寧願不訴諸厚顏無恥的解決辦法,堅持按照他們的設計做事。另外,這個錯誤可能暗示別的東西實際上是錯的,應該修正。
一些解決方案建議運行:
python manage.py migrate auth
python manage.py migrate
但是,這並沒有什麼,因爲有在我的項目沒有掛起的遷移。
我該如何解決這個神祕的問題?
那麼,他們有這種依賴性顯然'migrations.swappable_dependency (settings.AUTH_USER_MODEL)' – dabadaba
您是否有自定義用戶模型? – Alasdair
afaik我們使用的是Django的本地用戶。但是,我剛剛發現了這個事情,你認爲這很奇怪嗎?:這是我們模型中的一個屬性:'admins = models.ManyToManyField('auth.User')'。然後從shell我正在從數據庫中該模型的實例,而我打印instance.admins.first()',它輸出我所期待的'django.contrib.auth.models.User'的'類型。那麼如何將屬性定義爲'auth.User'?也許這是我們的Django升級帶來的一個錯誤?但是遷移甚至是如何起作用的呢?你看到什麼奇怪的嗎? – dabadaba