当前位置:  开发笔记 > 编程语言 > 正文

Django - 没有这样的表异常

如何解决《Django-没有这样的表异常》经验,为你挑选了3个好方法。

在Django中,我添加了一些模型models.py.之后manage.py makemigrations,manage.py migrate提出了这个例外:

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

所以我删除了所有旧的迁移makemigrationsmigrate再次运行,这似乎有效.

不幸的是,我注意到,它并没有帮助,因为当我尝试点击User customer profilesUser translator profiles它会引发异常:

环境:

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:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in wrapper
  618.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in inner
  233.             return view(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapper
  34.             return bound_func(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in bound_func
  30.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in changelist_view
  1550.                 self.list_max_show_all, self.list_editable, self)
File "C:\Python27\lib\site-packages\django\contrib\admin\views\main.py" in __init__
  82.         self.get_results(request)
File "C:\Python27\lib\site-packages\django\contrib\admin\views\main.py" in get_results
  177.         result_count = paginator.count
File "C:\Python27\lib\site-packages\django\core\paginator.py" in _get_count
  72.                 self._count = self.object_list.count()
File "C:\Python27\lib\site-packages\django\db\models\query.py" in count
  318.         return self.query.get_count(using=self.db)
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in get_count
  466.         number = obj.get_aggregation(using, ['__count'])['__count']
File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in get_aggregation
  447.         result = compiler.execute_sql(SINGLE)
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  840.             cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\utils.py" in __exit__
  98.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py" in execute
  64.                 return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.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'))

你知道问题出在哪里吗?谢谢



1> aks..:

我用这些步骤解决了同样的问题:

删除db.sqlite3项目目录中的数据库(在我的例子中)

__pycache__项目子目录下的文件夹中删除所有内容

您正在试图修复应用程序,进入该文件夹并清除migrations__pycache__目录

如果您确定已清除所有上述文件,请运行:

python manage.py makemigrations
python manage.py migrate

我希望这有帮助.


达到智慧结束:如果`manage makemigrations`是抛出"django.db.utils.OperationalError:没有这样的表:......"的错误?

2> Terry Brown..:

wihch的另一个案例可以生成没有这样的表错误.如果您的views.py或类似的代码执行在导入时尝试访问数据库的代码,即导入views.py有副作用,那么从头开始将不起作用.

当您的代码使用现有数据库时,会发生这种情况,现在您尝试在没有数据库的情况下启动.只需更改views.py即可导入而无需副作用.如果您不想修改设计,请执行以下操作:

from django.db.utils import OperationalError
format_list = [('', '(all)')]
geom_type_list = [('', '(all)')]
try:
    format_list.extend([(i[0],i[0]) 
        for i in Format.objects.values_list('name')])
    geom_type_list.extend([(i[0],i[0]) 
        for i in Geom_type.objects.values_list('name')])
except OperationalError:
    pass  # happens when db doesn't exist yet, views.py should be
          # importable without this side effect



3> kotrfa..:

再加上terry_brown的答案,这就是导致我遇到问题的原因。我有一个与ForeignKey到另一个模型的自定义用户模型。我将默认值设置为数据库中的第一个对象。当我将数据存储在数据库中时,它起作用了。但是,当我从头开始时,它不起作用,因为它在导入时立即执行了(因此甚至没有迁移完成)。

class User(AbstractBaseUser, PermissionsMixin):
    subscription_plan = models.ForeignKey(SubscriptionPlan, default=SubscriptionPlan.objects.first().id)

我不得不为此牺牲default(注释掉)。

更新:更好的解决方案是使用初始迁移或固定装置预填充数据库。


这里同样的问题。就我而言,我有一个试图在其构造函数中访问数据库的类。
推荐阅读
依然-狠幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有