ngx311 ngx311 - 3 months ago 11
Python Question

Django 1.9 migrate for the first time is not creating tables

I've never had this problem before. I've started countless projects and had no problem when I went to run 'makemigrations" for the first time. In this project I keep getting the error:


django.db.utils.OperationalError: no such table: accounts_customuser


That's the point, there isn't a table...it's supposed to make one.

I'm not running any fancy databases, it's just sqlite3.

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django_extensions',
'rest_framework',
'rest_framework.authtoken',
# 'djoser',
# 'django_comments',
# 'django_comments_xtd',
'accounts',
'blog',
]

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


Full TraceBack:

Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 4.5.4\helpers\pycharm\django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "C:\Python27\Lib\runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "C:\Python27\Lib\runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "C:\Python27\Lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Users\User\Desktop\newapp\manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
utility.execute()
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\management\__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\management\base.py", line 398, in execute
self.check()
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\management\base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
for pattern in resolver.url_patterns:
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\urlresolvers.py", line 417, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\utils\functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\core\urlresolvers.py", line 410, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Python27\Lib\importlib\__init__.py", line 37, in import_module
__import__(name)
File "C:/Users/User/Desktop/newapp\newapp\urls.py", line 21, in <module>
from blog.views import PostViewSet
File "C:/Users/User/Desktop/newapp\blog\views.py", line 14, in <module>
class PostViewSet(ModelViewSet):
File "C:/Users/User/Desktop/newapp\blog\views.py", line 15, in PostViewSet
some_user = CustomUser.objects.get(pk=1)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\models\manager.py", line 122, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\models\query.py", line 381, in get
num = len(clone)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\models\query.py", line 240, in __len__
self._fetch_all()
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\models\query.py", line 1074, in _fetch_all
self._result_cache = list(self.iterator())
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\models\query.py", line 52, in __iter__
results = compiler.execute_sql()
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\models\sql\compiler.py", line 848, in execute_sql
cursor.execute(sql, params)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\User\Desktop\newsite\lib\site-packages\django\db\backends\sqlite3\base.py", line 323, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: accounts_customuser


EDIT #2

Could the problem be in my PostViewSet? I've never done a complex query before, so I don't know if it's proper syntax. I got this from here

class PostViewSet(ModelViewSet):
some_user = CustomUser.objects.get(pk=1)
queryset = Post.objects.filter(
Q(poster=some_user) |
Q(poster__friends__creator=some_user) |
Q(poster__friendship_creator__friend=some_user)).distinct()
serializer_class = PostSerializer

Answer

The problem is that your CustomUser.objects.get(pk=1) query is running when the URL config is loaded and imports the views, before the tables have even been created. You can fix this by moving the code into a get_queryset method.

class PostViewSet(ModelViewSet):
    def get_queryset(self):
        some_user = CustomUser.objects.get(pk=1)
        return Post.objects.filter(
            Q(poster=some_user) |
            Q(poster__friends__creator=some_user) |
            Q(poster__friendship_creator__friend=some_user)).distinct()
    serializer_class = PostSerializer