yalinaksoy yalinaksoy - 16 days ago 7
Python Question

django_admin database creation, after sql command depreciated

I was using django 1.3 and python 2.4 for a big scale project. I decided to update it to django 1.9 and python 2.7.

Since django_admin's sql parameter is depreciated in 1.9, the update database creation method changed a lot.

I am using the command:

python /usr/local/django/<project>/manage.py migrate --fake-initial --noinput --run-syncdb


for table creation. But my tables are not created and manage is printing this error.

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")


There is nothing related with table creation in the error. It complains about missing the table but does not create it.

I am suspecting that
INSTALLED_APPS
does not include my tables, but I have appended my application to the list in my
settings.py
.
I got suspicious that I need to add my application separately to the
installed_apps
list.

I have followed recommended procedure by django itself while updating the system.

If my suspicions are true how could I add models to installed apps or any other suggestion would be great?

Answer

Thanks to django-user mailing list, I have realized my problem was trying to reference a not yet created table before creating it. "before it even starts to apply migrations, Django needs to import all your packages, so that it can determine what migrations it needs to apply. However, in the process of importing them, you are already executing database queries against those tables." Putting those reference fields into a try catch block catching the exception "ProgrammingError" solved my problem.