Django没有这样的表
Posted
技术标签:
【中文标题】Django没有这样的表【英文标题】:Django no such table 【发布时间】:2021-12-10 03:01:01 【问题描述】:在一个新的 Django 项目的开发环境中,我有 2 个应用程序,我遇到了“无列命名”异常的问题。我最终删除了 sqlite db 并重试了 python manage.py makemigrations
和 pyhon manage.py migrate
现在我遇到了一个没有表退出的新异常。我可以从sqlite模型验证该表没有退出,但是删除数据库后我没有收到任何错误并重新运行python manage.py makemigrations
和python manage.py migrate
在我的产品应用程序的 models.py 中
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")
在指令应用中的 models.py 中
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)
在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'
当我从管理员输入产品时输出错误
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
【问题讨论】:
试试python manage.py makemigrations app
和python manage.py migrate app
还有你的应用名称是什么?我认为您在settings.py
文件中添加了型号名称而不是应用名称。
这行得通!为什么不能像我一开始那样运行 python manage.py makemigrations?你能写一个答案来结束这个问题吗?
能否更新views.py
文件,以便我们更清楚地调试。
我还没有在views.py中写任何东西。它仅适用于管理员视图。
【参考方案1】:
正如 Sunil Ghimire python manage.py makemigrations app
和 python manage.py migrate app
所建议的那样,由于一些奇怪的原因,我还不明白 python manage.py makemigrations
和 python manage.py migrate
单独没有应用程序不起作用。
【讨论】:
以上是关于Django没有这样的表的主要内容,如果未能解决你的问题,请参考以下文章
OperationalError:没有这样的表:django_content_type 和 django_session