Django no such table error

In Django I added models into models.py. After manage.py makemigrations, manage.py migrate raised this exception: django.db.utils.OperationalError: no such table: auth_test_usertranslatorprofile I

In Django I added models into models.py. After manage.py makemigrations, manage.py migrate raised this exception:

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

I removed all old migrations and run makemigrations and migrate again which seemed to work. It didn’t help because when I click User customer profiles of User translator profiles it raises exception:

Request Method: GET
Request URL: http://127.0.0.1:8000/admin/auth_test/usertranslatorprofile/

Django Version: 1.8.7
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'auth_test')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "C:Python27libsite-packagesdjangocorehandlersbase.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:Python27libsite-packagesdjangocontribadminoptions.py" in wrapper
  618.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:Python27libsite-packagesdjangoutilsdecorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "C:Python27libsite-packagesdjangoviewsdecoratorscache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "C:Python27libsite-packagesdjangocontribadminsites.py" in inner
  233.             return view(request, *args, **kwargs)
File "C:Python27libsite-packagesdjangoutilsdecorators.py" in _wrapper
  34.             return bound_func(*args, **kwargs)
File "C:Python27libsite-packagesdjangoutilsdecorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "C:Python27libsite-packagesdjangoutilsdecorators.py" in bound_func
  30.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:Python27libsite-packagesdjangocontribadminoptions.py" in changelist_view
  1550.                 self.list_max_show_all, self.list_editable, self)
File "C:Python27libsite-packagesdjangocontribadminviewsmain.py" in __init__
  82.         self.get_results(request)
File "C:Python27libsite-packagesdjangocontribadminviewsmain.py" in get_results
  177.         result_count = paginator.count
File "C:Python27libsite-packagesdjangocorepaginator.py" in _get_count
  72.                 self._count = self.object_list.count()
File "C:Python27libsite-packagesdjangodbmodelsquery.py" in count
  318.         return self.query.get_count(using=self.db)
File "C:Python27libsite-packagesdjangodbmodelssqlquery.py" in get_count
  466.         number = obj.get_aggregation(using, ['__count'])['__count']
File "C:Python27libsite-packagesdjangodbmodelssqlquery.py" in get_aggregation
  447.         result = compiler.execute_sql(SINGLE)
File "C:Python27libsite-packagesdjangodbmodelssqlcompiler.py" in execute_sql
  840.             cursor.execute(sql, params)
File "C:Python27libsite-packagesdjangodbbackendsutils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)
File "C:Python27libsite-packagesdjangodbbackendsutils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "C:Python27libsite-packagesdjangodbutils.py" in __exit__
  98.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:Python27libsite-packagesdjangodbbackendsutils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "C:Python27libsite-packagesdjangodbbackendssqlite3base.py" in execute
  318.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /admin/auth_test/usertranslatorprofile/
Exception Value: no such table: auth_test_usertranslatorprofile

models.py:

from django.db import models
from django.contrib.auth.models import User

class Language(models.Model):
    shortcut = models.CharField(max_length=6)
    name = models.CharField(max_length=50)
    price_per_sign = models.FloatField()

class UserTranslatorProfile(models.Model):
    user = models.OneToOneField(User)
    languages = models.ManyToManyField(Language)
    price_per_word = models.FloatField()

class UserCustomerProfile(models.Model):
    user = models.OneToOneField(User)

admin.py:

from django import forms
from .models import Language
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class FreelancerRegistrationForm(forms.Form):
    language = forms.ModelChoiceField(queryset=Language.objects.all().order_by('shortcut'))

What is the problem?

Hi. I made models.py by watching your video, but I’m not sure how to solve this problem.
First of all, when I access to 127.0.0.1:8000/admin/chat/chat/add,
It shows

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

This is my models.py, and I migrated and deleted this models.py for several times.

from django.contrib.auth import get_user_model
from django.db import models

User = get_user_model()


# Create your models here.
class Contact(models.Model):
    user = models.ForeignKey(User, related_name='friends', on_delete=models.CASCADE)
    friends = models.ManyToManyField('self', blank=True)

    def __str__(self):
        return self.user.username


class Message(models.Model):
    contact = models.ForeignKey(Contact, related_name='messages', on_delete=models.CASCADE)
    content = models.TextField()
    timestamp = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.contact.user.username


class Chat(models.Model):
    participants = models.ManyToManyField(Contact, related_name='chats')
    messages = models.ManyToManyField(Message, blank=True)

    def last_10_messages(self):
        return self.messages.objects.order_by('-timestamp').all()[:10]

    def __str__(self):
        return "{}".format(self.pk)

And this is the Error Occurs.

Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/chat/chat/add/

Django Version: 3.0.4
Python Version: 3.6.5
Installed Applications:
[‘channels’,
‘chat’,
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘django.contrib.sites’,
‘allauth’,
‘allauth.account’,
‘allauth.socialaccount’,
‘corsheaders’,
‘rest_auth’,
‘rest_auth.registration’,
‘rest_framework’,
‘rest_framework.authtoken’]
Installed Middleware:
[‘corsheaders.middleware.CorsMiddleware’,
‘django.middleware.security.SecurityMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
‘django.middleware.common.CommonMiddleware’,
‘django.middleware.csrf.CsrfViewMiddleware’,
‘django.contrib.auth.middleware.AuthenticationMiddleware’,
‘django.contrib.messages.middleware.MessageMiddleware’,
‘django.middleware.clickjacking.XFrameOptionsMiddleware’]

Template error:
In template E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangocontribadmintemplatesadminincludesfieldset.html, error at line 19
no such table: chat_contact
9 : {% for field in line %}
10 : <div{% if not line.fields|length_is:’1′ %} class=»fieldBox{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}{% if field.field.is_hidden %} hidden{% endif %}»{% elif field.is_checkbox %} class=»checkbox-row»{% endif %}>
11 : {% if not line.fields|length_is:’1′ and not field.is_readonly %}{{ field.errors }}{% endif %}
12 : {% if field.is_checkbox %}
13 : {{ field.field }}{{ field.label_tag }}
14 : {% else %}
15 : {{ field.label_tag }}
16 : {% if field.is_readonly %}
17 :

{{ field.contents }}

18 : {% else %}
19 : {{ field.field }}
20 : {% endif %}
21 : {% endif %}
22 : {% if field.field.help_text %}
23 :

{{ field.field.help_text|safe }}

24 : {% endif %}
25 :
26 : {% endfor %}
27 :
28 : {% endfor %}
29 :

Traceback (most recent call last):
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangodbbackendsutils.py», line 86, in _execute
return self.cursor.execute(sql, params)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangodbbackendssqlite3base.py», line 396, in execute
return Database.Cursor.execute(self, query, params)

The above exception (no such table: chat_contact) was the direct cause of the following exception:
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangocorehandlersexception.py», line 34, in inner
response = get_response(request)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangocorehandlersbase.py», line 145, in _get_response
response = self.process_exception_by_middleware(e, request)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangocorehandlersbase.py», line 143, in _get_response
response = response.render()
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplateresponse.py», line 105, in render
self.content = self.rendered_content
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplateresponse.py», line 83, in rendered_content
return template.render(context, self._request)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebackendsdjango.py», line 61, in render
return self.template.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 171, in render
return self._render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 163, in _render
return self.nodelist.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 936, in render
bit = node.render_annotated(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 903, in render_annotated
return self.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplateloader_tags.py», line 150, in render
return compiled_parent._render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 163, in _render
return self.nodelist.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 936, in render
bit = node.render_annotated(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 903, in render_annotated
return self.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplateloader_tags.py», line 150, in render
return compiled_parent._render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 163, in _render
return self.nodelist.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 936, in render
bit = node.render_annotated(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 903, in render_annotated
return self.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplateloader_tags.py», line 62, in render
result = block.nodelist.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 936, in render
bit = node.render_annotated(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 903, in render_annotated
return self.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplateloader_tags.py», line 62, in render
result = block.nodelist.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 936, in render
bit = node.render_annotated(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 903, in render_annotated
return self.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatedefaulttags.py», line 209, in render
nodelist.append(node.render_annotated(context))
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 903, in render_annotated
return self.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplateloader_tags.py», line 188, in render
return template.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 173, in render
return self._render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 163, in _render
return self.nodelist.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 936, in render
bit = node.render_annotated(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 903, in render_annotated
return self.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatedefaulttags.py», line 209, in render
nodelist.append(node.render_annotated(context))
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 903, in render_annotated
return self.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatedefaulttags.py», line 209, in render
nodelist.append(node.render_annotated(context))
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 903, in render_annotated
return self.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatedefaulttags.py», line 309, in render
return nodelist.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 936, in render
bit = node.render_annotated(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 903, in render_annotated
return self.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatedefaulttags.py», line 309, in render
return nodelist.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 936, in render
bit = node.render_annotated(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 903, in render_annotated
return self.render(context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 992, in render
return render_value_in_context(output, context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangotemplatebase.py», line 971, in render_value_in_context
value = str(value)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangoutilshtml.py», line 373, in
klass.str = lambda self: mark_safe(klass_str(self))
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangoformsboundfield.py», line 33, in str
return self.as_widget()
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangoformsboundfield.py», line 96, in as_widget
renderer=self.form.renderer,
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangoformswidgets.py», line 241, in render
context = self.get_context(name, value, attrs)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangocontribadminwidgets.py», line 288, in get_context
‘rendered_widget’: self.widget.render(name, value, attrs),
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangoformswidgets.py», line 241, in render
context = self.get_context(name, value, attrs)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangoformswidgets.py», line 678, in get_context
context = super().get_context(name, value, attrs)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangoformswidgets.py», line 639, in get_context
context[‘widget’][‘optgroups’] = self.optgroups(name, context[‘widget’][‘value’], attrs)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangoformswidgets.py», line 587, in optgroups
for index, (option_value, option_label) in enumerate(self.choices):
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangoformsmodels.py», line 1140, in iter
for obj in queryset:
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangodbmodelsquery.py», line 346, in _iterator
yield from self._iterable_class(self, chunked_fetch=use_chunked_fetch, chunk_size=chunk_size)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangodbmodelsquery.py», line 57, in iter
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangodbmodelssqlcompiler.py», line 1151, in execute_sql
cursor.execute(sql, params)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangodbbackendsutils.py», line 100, in execute
return super().execute(sql, params)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangodbbackendsutils.py», line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangodbbackendsutils.py», line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangodbbackendsutils.py», line 86, in _execute
return self.cursor.execute(sql, params)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangodbutils.py», line 90, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangodbbackendsutils.py», line 86, in _execute
return self.cursor.execute(sql, params)
File «E:DjangoDjango_ChannelsNew_Channelslibsite-packagesdjangodbbackendssqlite3base.py», line 396, in execute
return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /admin/chat/chat/add/
Exception Value: no such table: chat_contact

image

How should I solve this??

While setting up an app using Django REST Framework, after configuration settings.py and models.py. After setting up an initial migration for our person model, sync the database and serializer changes. When you try to load the project and if you see an error related to Django operational error at no such table ‘persons_person’. Below is the error, you might see while running the app.

OperationalError at /persons/
no such table: persons_person
Request Method: GET
Request URL: http://127.0.0.1:8000/persons/
Django Version: 1.8.3
Exception Type: OperationalError
Exception Value:
no such table: persons_person
Exception Location: /usr/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py in execute, line 318
Python Executable: /usr/local/opt/python/bin/python2.7
Python Version: 2.7.9
Python Path:
[‘/djangoRest’,
‘/usr/local/lib/python2.7/site-packages/setuptools-12.0.5-py2.7.egg’,
‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python27.zip’,
‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7’,
‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin’,
‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac’,
‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages’,
‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk’,
‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old’,
‘/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload’,
‘/usr/local/lib/python2.7/site-packages’,
‘/Library/Python/2.7/site-packages/RBTools-0.6.2-py2.7.egg’,
‘/Library/Python/2.7/site-packages/requests-2.4.0-py2.7.egg’,
‘/Library/Python/2.7/site-packages/certifi-14.05.14-py2.7.egg’,
‘/Library/Python/2.7/site-packages’]

Solution:

rm -rf migrations
rm db.sqlite3
python manage.py makemigrations persons
python manage.py migrate

1. Delete the migrations folder.
2. Delete the db sqlite file.
3. Create an initial migration for persons.
4. Sync the database.

Понравилась статья? Поделить с друзьями:
  • Django nginx 500 internal server error
  • Django models error messages
  • Django migrations error
  • Django login form error
  • Django logging error 500