开发者

Django syncdb error

I have django.contrib.auth in my installed apps and everything was was working about 10 minutes ago. I deleted the existed database because I had problems with south migrations. When I try to rebuild it I get an error.

Error: django.db.utils.DatabaseError: no such table: 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开发者_Go百科/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**


The problem is that you are running a query against the User 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)

In the syncdb process (during field declaration actually, so any time your module is imported, like during this validation process).

This will make sure that the auth_user query is executed before the auth_user table is created.

A safer way to get the functionality with the default user is to do

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

And then in your field declaration, do

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

As per the model field reference default can be a callable. Note that you would have the same problem if you used get_default_user() in there - then it executes immediately, not on-demand.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜