我正在开发一个Intranet Web应用程序,在我的开发环境中一切正常,但是当我尝试在我的服务器上部署项目并使用makemigrations启动数据库迁移时,我得到“app.table不存在”的错误。
我面临的问题是,确实,表格不存在,因为它是第一次进行的迁移,错误出现在我的forms.py文件中。
我试过了:
- Deleting the app/migrations folder, didnt helped
- Executing "python manage.py migrate --fake", same result
我确实创建了数据库
修改
中触发错误/path/to/my/project/app/forms.py, in class Meta:
编辑#2
跟踪错误日志,似乎与project / project / urls.py有关。 我从app.forms导入LoginForm和PasswordChange。我认为这是在进行初始迁移之前触发对数据库的一些查询。
回溯
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 398, in execute
self.check()
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/usr/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/lib/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/usr/lib/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/usr/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/rortega/smce/smce/urls.py", line 20, in <module>
from employeesControll.forms import LoginForm, PasswordChange
File "/home/rortega/smce/employeesControll/forms.py", line 60, in <module>
class employeesForm(forms.ModelForm):
File "/home/rortega/smce/employeesControll/forms.py", line 61, in employeesForm
class Meta:
File "/home/rortega/smce/employeesControll/forms.py", line 96, in Meta
'id_costCenter': forms.Select(choices=ccOptions, attrs={'class': 'form-control', 'name': 'id_costCenter'}),
File "/usr/lib/python2.7/site-packages/django/forms/widgets.py", line 514, in __init__
self.choices = list(choices)
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
self._fetch_all()
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 128, in __iter__
for row in compiler.results_iter():
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 802, in results_iter
results = self.execute_sql(MULTI)
File "/usr/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 112, in execute
return self.cursor.execute(query, args)
File "/usr/lib64/python2.7/site-packages/MySQLdb/cursors.py", line 226, in execute
self.errorhandler(self, exc, value)
File "/usr/lib64/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorvalue
django.db.utils.ProgrammingError: (1146, "Table 'smce_db.employeesControll_costcenter' doesn't exist")
答案 0 :(得分:2)
我找不到任何“pythonic”解决方案,但我可以通过在我的views.py文件中注释任何对form.py这样的引用来解决迁移问题:
class JobcodeCreate(CreateView):
# form_class = jobcodeForm
model = JobCode
template_name = 'employeesControll/jobcode_create_form.html'
success_url = reverse_lazy('jobcode-add')
并在导入这些表单时创建if语句:
import sys
if 'makemigrations' not in sys.argv and 'migrate' not in sys.argv:
from .forms import (
jobcodeForm,
costcenterForm,
employeesForm,
employeesPersonalForm as form_EP,
emergencyContactForm as form_EC
)
我不知道这是唯一还是最好的解决方案,但只要它能够正常工作,我就可以随时随地编写代码。
显然这是1.9版本更新的一个问题,因为从这个版本开始,django在进行迁移之前会加载urls.py,所以如果加载了urls.py,还有views.py,如果你使用form_class来自定义表单的类,当forms.py试图检查数据库时,你会得到这个错误(疯了,我知道)。
答案 1 :(得分:0)
使用--fake-initial
阻止Django首次尝试迁移时抱怨app.table
不存在。
https://docs.djangoproject.com/en/1.10/topics/migrations/#initial-migrations