jxw
jxw

Reputation: 711

Django no such table

In the development environment of a new Django project I have 2 apps and I am was running into issues of "no column named" exceptions. I ended up deleting the sqlite db and retried python manage.py makemigrations and pyhon manage.py migrate now I run into a new exception of no table exits. I can verify from the sqlite model that the table do not exits, but I did not receive any errors after deleting the database and rerun python manage.py makemigrations and python manage.py migrate

In models.py for my products app

    from django.db import models
    from django.db.models.fields import CharField
    from instructions.models import Instruction
    # Create your models here.
    
    class Product(models.Model):
        productName = models.CharField(max_length=200)
        productDescription = models.TextField(blank=True)
        productPrice = models.DecimalField(max_digits=9, decimal_places=0, default=0)
        productSubQuantity = models.IntegerField(default=1, blank=False)
        productAvailable = models.BooleanField(default = True)
        productCreated_at = models.DateTimeField(auto_now_add = True)
        productUpdated_at = models.DateTimeField(auto_now = True)
        productSlug = models.SlugField(max_length = 255, unique = True,
        help_text = "Unique text for url created from product name")
        productMetaKeywords = models.CharField("Meta keywords for SEO", max_length = 255,
        help_text ="Comma delimited words for SEO")
        productMetaDescription = models.CharField("Meta description", max_length = 255,
        help_text="Content for meta tag description")
        instruction = models.ForeignKey(Instruction, on_delete = models.CASCADE, related_name = "instruction")
    
        class Meta:
            db_table = 'products'
            ordering = ['-productName']
    
        def __str__(self):
            return self.productName
    
    class Image(models.Model):
        image = models.ImageField()
        imageMetaKeyWords = models.CharField("Meta keywords for SEO", max_length = 255,
        help_text = "Comma delimited words for SEO")
        imageMetaDescription = models.CharField("Meta description", max_length = 255,
        help_text = "Content for image meta tag description")
        defaultImage = models.BooleanField(default= False)
        product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name="images")

In models.py in instructions app

from django.db import models

# Create your models here.
class Instruction(models.Model):
    instructionName = models.CharField(max_length=200)
    instructionEstimatedPreparationTime = models.IntegerField(default = 0)
    instructionContent = models.TextField(blank = False)
    instructionVideoFilePath = models.FileField(blank = True, null = True)
    instructionVideoQRcode = models.CharField(max_length=1000, blank = True, null = True)
    

In settings.py

"""
Django settings for dimsumbox project.

Generated by 'django-admin startproject' using Django 3.2.8.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-)4we8*9#lmu^+(x%k+&thxt8t0f=*m^f8h5xfba9wiwc6r2@ze'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'products',
    'instructions'
]

MIDDLEWARE = [
    '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',
]

ROOT_URLCONF = 'dimsumbox.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'dimsumbox.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

Error output when I entered the products from admin

Internal Server Error: /admin/products/product/
Traceback (most recent call last):
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: products

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/contrib/admin/options.py", line 616, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/contrib/admin/sites.py", line 232, in inner
    return view(request, *args, **kwargs)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/contrib/admin/options.py", line 1697, in changelist_view
    cl = self.get_changelist_instance(request)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/contrib/admin/options.py", line 736, in get_changelist_instance
    return ChangeList(
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/contrib/admin/views/main.py", line 100, in __init__
    self.get_results(request)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/contrib/admin/views/main.py", line 235, in get_results
    result_count = paginator.count
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/core/paginator.py", line 97, in count
    return c()
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/models/query.py", line 412, in count
    return self.query.get_count(using=self.db)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/models/sql/query.py", line 519, in get_count
    number = obj.get_aggregation(using, ['__count'])['__count']
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/models/sql/query.py", line 504, in get_aggregation
    result = compiler.execute_sql(SINGLE)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
    cursor.execute(sql, params)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/utils.py", line 79, in _execute
    with self.db.wrap_database_errors:
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
(env) jianxiongwu@Jians-MacBook-Pro dimsumbox % python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
October 24, 2021 - 06:09:35
Django version 3.2.8, using settings 'dimsumbox.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[24/Oct/2021 06:09:40] "GET / HTTP/1.1" 200 10697
[24/Oct/2021 06:09:43] "GET /admin/ HTTP/1.1" 200 5130
[24/Oct/2021 06:09:44] "GET /admin/ HTTP/1.1" 200 5130
Internal Server Error: /admin/products/product/
Traceback (most recent call last):
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: products

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/contrib/admin/options.py", line 616, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/contrib/admin/sites.py", line 232, in inner
    return view(request, *args, **kwargs)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/contrib/admin/options.py", line 1697, in changelist_view
    cl = self.get_changelist_instance(request)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/contrib/admin/options.py", line 736, in get_changelist_instance
    return ChangeList(
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/contrib/admin/views/main.py", line 100, in __init__
    self.get_results(request)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/contrib/admin/views/main.py", line 235, in get_results
    result_count = paginator.count
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/core/paginator.py", line 97, in count
    return c()
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/models/query.py", line 412, in count
    return self.query.get_count(using=self.db)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/models/sql/query.py", line 519, in get_count
    number = obj.get_aggregation(using, ['__count'])['__count']
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/models/sql/query.py", line 504, in get_aggregation
    result = compiler.execute_sql(SINGLE)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1175, in execute_sql
    cursor.execute(sql, params)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/utils.py", line 98, in execute
    return super().execute(sql, params)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/utils.py", line 79, in _execute
    with self.db.wrap_database_errors:
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/jianxiongwu/Documents/Python/Github/DimsumBox_website/env/lib/python3.10/site-packages/django/db/backends/sqlite3/base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: products
[24/Oct/2021 06:09:46] "GET /admin/products/product/ HTTP/1.1" 500 170815

Upvotes: 0

Views: 4803

Answers (2)

user21880536
user21880536

Reputation: 1

May be migrations directory missing in app folder, that may be the reason. That why when we add app name at the end of makemigrations , it creates an migrations directory in app

Upvotes: 0

jxw
jxw

Reputation: 711

As suggested by Sunil Ghimire python manage.py makemigrations app followed by python manage.py migrate app solved this problem as for some strange reasons that I don't yet understand the python manage.py makemigrations and python manage.py migrate alone without the app did not work.

Upvotes: 3

Related Questions