django_admin 数据库创建,sql 命令弃用后
django_admin database creation, after sql command depreciated
我在大型项目中使用 django 1.3 和 python 2.4。我决定将它更新到 django 1.9 和 python 2.7.
由于django_admin的sql参数在1.9中被弃用,更新数据库的创建方式发生了很大变化。
我正在使用命令:
python /usr/local/django/<project>/manage.py migrate --fake-initial --noinput --run-syncdb
用于 table 创建。但是我的 table 没有创建,管理正在打印此错误。
Traceback (most recent call last):
File "/src/project/project-export/django/projectadmin/manage.py", line 13, in <module>
execute_from_command_line(sys.argv)
File "/opt/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/opt/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/python/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/python/lib/python2.7/site-packages/django/core/management/base.py", line 398, in execute
self.check()
File "/opt/python/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/opt/python/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/opt/python/lib/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/opt/python/lib/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/opt/python/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/opt/python/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/opt/python/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/opt/python/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/opt/python/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/usr/local/django/projectadmin/projectadmin/urls.py", line 4, in <module>
import projectadmin.views
File "/usr/local/django/projectadmin/projectadmin/views.py", line 20, in <module>
from projectadmin.forms import UserCreationForm
File "/usr/local/django/projectadmin/projectadmin/forms.py", line 252, in <module>
class UserCreationForm(forms.ModelForm):
File "/usr/local/django/projectadmin/projectadmin/forms.py", line 256, in UserCreationForm
template_choice = form_utils.create_template_choices_field()
File "/usr/local/django/projectadmin/projectadmin/form_utils.py", line 15, in create_template_choices_field
choices = [(x.id, x.template_name) for x in all_templates]
File "/opt/python/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
self._fetch_all()
File "/opt/python/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "/opt/python/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
results = compiler.execute_sql()
File "/opt/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "/opt/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/opt/python/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/opt/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/opt/python/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 112, in execute
return self.cursor.execute(query, args)
File "/opt/python/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/opt/python/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.ProgrammingError: (1146, "Table 'project.projectadmin_usertemplate' doesn't exist")
错误中没有与 table 创建相关的内容。它抱怨缺少 table 但没有创建它。
我怀疑 INSTALLED_APPS
不包括我的 table,但我已将我的申请附加到 settings.py
的列表中。
我怀疑我需要将我的应用程序单独添加到 installed_apps
列表中。
我在更新系统时遵循了django本身推荐的程序。
如果我的怀疑是真的,我该如何将模型添加到已安装的应用程序或任何其他建议都很好?
感谢 django-user 邮件列表,我意识到我的问题是在创建之前试图引用一个尚未创建的 table。
“甚至在开始应用迁移之前,Django
需要导入所有包,以便它可以确定什么
它需要应用的迁移。但是在导入的过程中
他们,您已经在对那些 table 执行数据库查询。”
将这些引用字段放入捕获异常的 try catch 块中 "ProgrammingError" 解决了我的问题。
我在大型项目中使用 django 1.3 和 python 2.4。我决定将它更新到 django 1.9 和 python 2.7.
由于django_admin的sql参数在1.9中被弃用,更新数据库的创建方式发生了很大变化。
我正在使用命令:
python /usr/local/django/<project>/manage.py migrate --fake-initial --noinput --run-syncdb
用于 table 创建。但是我的 table 没有创建,管理正在打印此错误。
Traceback (most recent call last):
File "/src/project/project-export/django/projectadmin/manage.py", line 13, in <module>
execute_from_command_line(sys.argv)
File "/opt/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/opt/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/python/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/python/lib/python2.7/site-packages/django/core/management/base.py", line 398, in execute
self.check()
File "/opt/python/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/opt/python/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/opt/python/lib/python2.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/opt/python/lib/python2.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "/opt/python/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/opt/python/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/opt/python/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/opt/python/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "/opt/python/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/usr/local/django/projectadmin/projectadmin/urls.py", line 4, in <module>
import projectadmin.views
File "/usr/local/django/projectadmin/projectadmin/views.py", line 20, in <module>
from projectadmin.forms import UserCreationForm
File "/usr/local/django/projectadmin/projectadmin/forms.py", line 252, in <module>
class UserCreationForm(forms.ModelForm):
File "/usr/local/django/projectadmin/projectadmin/forms.py", line 256, in UserCreationForm
template_choice = form_utils.create_template_choices_field()
File "/usr/local/django/projectadmin/projectadmin/form_utils.py", line 15, in create_template_choices_field
choices = [(x.id, x.template_name) for x in all_templates]
File "/opt/python/lib/python2.7/site-packages/django/db/models/query.py", line 258, in __iter__
self._fetch_all()
File "/opt/python/lib/python2.7/site-packages/django/db/models/query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "/opt/python/lib/python2.7/site-packages/django/db/models/query.py", line 52, in __iter__
results = compiler.execute_sql()
File "/opt/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "/opt/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/opt/python/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/opt/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/opt/python/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 112, in execute
return self.cursor.execute(query, args)
File "/opt/python/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/opt/python/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.ProgrammingError: (1146, "Table 'project.projectadmin_usertemplate' doesn't exist")
错误中没有与 table 创建相关的内容。它抱怨缺少 table 但没有创建它。
我怀疑 INSTALLED_APPS
不包括我的 table,但我已将我的申请附加到 settings.py
的列表中。
我怀疑我需要将我的应用程序单独添加到 installed_apps
列表中。
我在更新系统时遵循了django本身推荐的程序。
如果我的怀疑是真的,我该如何将模型添加到已安装的应用程序或任何其他建议都很好?
感谢 django-user 邮件列表,我意识到我的问题是在创建之前试图引用一个尚未创建的 table。 “甚至在开始应用迁移之前,Django 需要导入所有包,以便它可以确定什么 它需要应用的迁移。但是在导入的过程中 他们,您已经在对那些 table 执行数据库查询。” 将这些引用字段放入捕获异常的 try catch 块中 "ProgrammingError" 解决了我的问题。