Django设置:无此类表auth_user

时间:2018-10-21 12:32:38

标签: django django-models django-deployment

每次我们在新环境中设置Django项目并尝试启动服务器(migraterunserver)时,都会显示错误消息django.db.utils.OperationalError: no such table: auth_user(下面是完整的错误消息)。

我假设这是由于在某些模型中将用户模型用作外键(如下所示),并且随着新系统的建立(尚未创建数据库),Django尝试为自定义表创建表建模,并由于尚未创建外键-用户表而失败。

# Model for projects (example)
class Projekt(models.Model):
    project_name = models.CharField(max_length=100, unique=True)
    project_manager = models.ForeignKey(User, related_name="Manager", on_delete=models.DO_NOTHING)

我的问题是:有什么最佳实践可以防止这种情况发生?目前,我们只是为正在获得的每个新开发人员从正在运行的系统中复制数据库,该数据库目前可以运行,但不适用于该软件的开源分发。

  

我已经尝试了类似thisthis)之类的东西,但是没有成功。我在每个Django项目中都遇到了这个问题。


USERNAME$ python3 manage.py migrate
  Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
      return self.cursor.execute(sql, params)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 296, in execute
      return Database.Cursor.execute(self, query, params)
  sqlite3.OperationalError: no such table: auth_user

  The above exception was the direct cause of the following exception:

  Traceback (most recent call last):
    File "manage.py", line 15, in <module>
      execute_from_command_line(sys.argv)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
      utility.execute()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
      self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
      self.execute(*args, **cmd_options)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 350, in execute
      self.check()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 379, in check
      include_deployment_checks=include_deployment_checks,
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 60, in _run_checks
      issues.extend(super()._run_checks(**kwargs))
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 366, in _run_checks
      return checks.run_checks(**kwargs)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/checks/registry.py", line 71, in run_checks
      new_errors = check(app_configs=app_configs)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
      all_namespaces = _load_all_namespaces(resolver)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
      url_patterns = getattr(resolver, 'url_patterns', [])
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
      res = instance.__dict__[self.name] = self.func(instance)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/urls/resolvers.py", line 533, in url_patterns
      patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/utils/functional.py", line 37, in __get__
      res = instance.__dict__[self.name] = self.func(instance)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
      return import_module(self.urlconf_name)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
    File "<frozen importlib._bootstrap>", line 983, in _find_and_load
    File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
    File "<frozen importlib._bootstrap_external>", line 728, in exec_module
    File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
    File "/USERNAME/Documents/PROJECTS/PROJECTFOLDER/PROJECT_NAME/urls.py", line 8, in <module>
      path('i/', include('APP1.urls')), # Private space/ Development space
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include
      urlconf_module = import_module(urlconf_module)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
    File "<frozen importlib._bootstrap>", line 983, in _find_and_load
    File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
    File "<frozen importlib._bootstrap_external>", line 728, in exec_module
    File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
    File "/USERNAME/Documents/PROJECTS/PROJECTFOLDER/APP1/urls.py", line 4, in <module>
      from . import views
    File "/USERNAME/Documents/PROJECTS/PROJECTFOLDER/APP1/views.py", line 11, in <module>
      from .forms import AddProject, AddTicket
    File "/USERNAME/Documents/PROJECTS/PROJECTFOLDER/APP1/forms.py", line 5, in <module>
      class AddProject(forms.Form):
    File "/USERNAME/Documents/PROJECTS/PROJECTFOLDER/APP1/forms.py", line 7, in AddProject
      project_manager = forms.ChoiceField(choices=[(user.id, user) for user in User.objects.all()])
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/query.py", line 268, in __iter__
      self._fetch_all()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/query.py", line 1186, in _fetch_all
      self._result_cache = list(self._iterable_class(self))
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/query.py", line 54, in __iter__
      results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1065, in execute_sql
      cursor.execute(sql, params)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 100, in execute
      return super().execute(sql, params)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
      return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
      return executor(sql, params, many, context)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
      return self.cursor.execute(sql, params)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
      raise dj_exc_value.with_traceback(traceback) from exc_value
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
      return self.cursor.execute(sql, params)
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 296, in execute
      return Database.Cursor.execute(self, query, params)
  django.db.utils.OperationalError: no such table: auth_user

4 个答案:

答案 0 :(得分:3)

将Migrations文件夹添加到git。其中包含正确创建新数据库所需的信息。

答案 1 :(得分:1)

应该将您的迁移添加到版本控制中,并将其与其余代码库一起部署。

如果现在有很多迁移是多余的,则可以squash

答案 2 :(得分:0)

我可能回答这个问题的时间很晚,但是我所做的事情也一样,从应用程序中删除了pycache文件以及相关的迁移文件,然后在我的sqlite文件中也删除了数据库文件

答案 3 :(得分:0)

我正在使用PyCharm,并且在调试配置中错误地设置了工作目录。需要指向其中包含manage.py的目录。也许不是creyD的问题,但可能会帮助其他人。