Izuzvo Izuzvo - 1 month ago 4x
MySQL Question

Django 1.9 | makemigrations on production server fails

Im developing an intranet web app, everything is working fine in my development enviroment, but when i try to deploy the project on my server and start the database migration with makemigrations i get the error that "app.table doesnt exist".

The problem im facing is that indeed, the table doesnt exist because its the first migration im making, and the error is presented in my forms.py file.

I tried:

- Deleting the app/migrations folder, didnt helped
- Executing "python manage.py migrate --fake", same result

And i do have created the database

The error is triggered in

/path/to/my/project/app/forms.py, in class Meta:

Edit #2

Tracking the log of the error, it seems to be something with project/project/urls.py.
Im importing from app.forms the LoginForm and PasswordChange. i think this is triggering some query to the database before making the initial migrations.


Traceback (most recent call last):
File "manage.py", line 10, in <module>
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 345, in execute
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
File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 426, in check
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
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__
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")


I couldn’t find any "pythonic" solution, but I could solve the migration problem by commenting in my views.py file any reference I had to forms.py like this:

class JobcodeCreate(CreateView):
    # form_class = jobcodeForm
    model = JobCode
    template_name = 'employeesControll/jobcode_create_form.html'
    success_url = reverse_lazy('jobcode-add')

and also creating an if statement when I import those forms:

import sys
if 'makemigrations' not in sys.argv and 'migrate' not in sys.argv:
    from .forms import (
        employeesPersonalForm as form_EP,
        emergencyContactForm as form_EC

I don’t know if this is the only or the best solution, but as long as it worked and i can keep coding im cool with it.

Apparently this is an issue from 1.9 version update because, since this version, django loads the urls.py before making the migrations, so if urls.py is loaded, also views.py, and if you use form_class to customize the class of the form, you'll get this error when forms.py tries to check the database (crazy, I know).