2011-04-29 131 views
2

我在我安裝的應用程序中有django.contrib.auth,一切都在10分鐘前工作。我刪除了現有的數據庫,因爲我在南遷移時遇到了問題。當我嘗試重建它時,出現錯誤。Django syncdb錯誤

錯誤:django.db.utils.DatabaseError:沒有這樣的表:AUTH_USER

Traceback (most recent call last): 
    File "manage.py", line 11, in <module> 
    execute_manager(settings) 
    File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager 
    utility.execute() 
    File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv 
    self.execute(*args, **options.__dict__) 
    File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 219, in execute 
    self.validate() 
    File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 249, in validate 
    num_errors = get_validation_errors(s, app) 
    File "/usr/lib/python2.7/site-packages/django/core/management/validation.py", line 35, in get_validation_errors 
    for (app_name, error) in get_app_errors().items(): 
    File "/usr/lib/python2.7/site-packages/django/db/models/loading.py", line 146, in get_app_errors 
    self._populate() 
    File "/usr/lib/python2.7/site-packages/django/db/models/loading.py", line 61, in _populate 
    self.load_app(app_name, True) 
    File "/usr/lib/python2.7/site-packages/django/db/models/loading.py", line 78, in load_app 
    models = import_module('.models', app_name) 
    File "/usr/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module 
    __import__(name) 
    File "/home/bruk/workspace/hungryDroid/src/hungryDroid/Ingredient/models.py", line 22, in <module> 
    class Ingredient(models.Model): 
    File "/home/bruk/workspace/hungryDroid/src/hungryDroid/Ingredient/models.py", line 26, in Ingredient 
    FKowner = models.ForeignKey(User, default=User.objects.get(pk=1).id) 
    File "/usr/lib/python2.7/site-packages/django/db/models/manager.py", line 132, in get 
    return self.get_query_set().get(*args, **kwargs) 
    File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 342, in get 
    num = len(clone) 
    File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 80, in __len__ 
    self._result_cache = list(self.iterator()) 
    File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 271, in iterator 
    for row in compiler.results_iter(): 
    File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 677, in results_iter 
    for rows in self.execute_sql(MULTI): 
    File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 732, in execute_sql 
    cursor.execute(sql, params) 
    File "/usr/lib/python2.7/site-packages/django/db/backends/util.py", line 15, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 200, in execute 
    return Database.Cursor.execute(self, query, params) 
**django.db.utils.DatabaseError: no such table: auth_user** 
+0

我看到了同樣的錯誤,因爲我在我的TestCase的__init__函數中抓取了一個虛擬用戶,這發生在數據庫準備好之前(正如Henry指出的那樣)。在我的情況下,將用戶操作移動到測試功能解決了問題。 – dbn 2012-10-17 18:08:21

回答

8

的問題是,你正在運行鍼對用戶模型

File "/home/bruk/workspace/hungryDroid/src/hungryDroid/Ingredient/models.py", line 26, in Ingredient 
FKowner = models.ForeignKey(User, default=User.objects.get(pk=1).id) 

查詢在syncdb進程(實際上在字段聲明期間,所以任何時候導入你的模塊,就像在驗證過程中一樣)。

這將確保在創建auth_user表之前執行auth_user查詢。

一個更安全的方式來獲得與默認用戶的功能就是做

def get_default_user(): 
    return User.objects.get(pk=1) 

,然後在現場報關,做

FKowner = models.ForeignKey(User, default=get_default_user) 

按照該model field reference缺省值可以是可調用的。請注意,如果您在那裏使用了get_default_user(),那麼您將有相同的問題 - 然後它立即執行,而不是按需執行。

+0

感謝您的快速和詳細的回覆。我能夠通過刪除應用程序和構建數據庫來避開它。然後,在我擁有user_auth表後,將其添加回我安裝的應用程序。但是現在我明白髮生了什麼,並會相應地做出改變。再次感謝。 – 2011-04-29 03:05:59