Monday, November 21, 2016

Re: django_admin database creation, after sql command depreciated



On Friday, November 18, 2016 at 7:36:05 PM UTC+3, Michal Petrucha wrote:
On Fri, Nov 18, 2016 at 12:07:40AM -0800, Yalın Aksoy wrote:
>
>
> Hello,
>
>
> 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 makemigrations"
>
>  
>
> > "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 in both commands.
>
>  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
> >     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=include_deployment_checks,
> >   File "/opt/python/lib/python2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
> >     new_errors
> >     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
> >     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
> >     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
> >     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__
> >     __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
> >     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
> >     results = compiler.execute_sql()
> >   File "/opt/python/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 848, in execute_sql
> >     cursor
> >     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
> >     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
> > django.db.utils.ProgrammingError: (1146, "Table 'project.projectadmin_usertemplate' doesn't exist")
>
>
> There is nothing related with table creation in the error or in any
> logs. It complains about missing the table but does not create it.
>
> I am suspecting that INSTALLED_APPS does not include my tables even
> I am sure it includes my projectadmin, 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.

Nothing like that. As you noted above, you are getting the error about
a missing table before Django ever gets a chance to start applying the
migrations that would create that table.

If you look at the traceback closely, you'll notice that in file
"/usr/local/django/projectadmin/projectadmin/form_utils.py", line 15,
in create_template_choices_field, you are iterating over a queryset
while that module is being imported.

Think about 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.

It looks like you are generating a list of choices dynamically in a
form field, based on some model. For cases like this, you should most
likely be using a ModelChoiceField, which avoids the problem of
import-time queries (as well as the problem that the list of choices
will not be updated to reflect changes in the set of model objects
unless you restart your server) by querying the database only when
it's actually used.

Good luck,

Michal

Hello Michal,

Thanks for the pointing out the mistake, I really was trying to create a table by referencing another table which does not created yet. Checking those initialization s solved my problem.  

Thank you so much
-Yalin

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/acf90ebe-d5c8-4808-9d73-e899b0c4abc0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment