django.db.utils.ProgrammingError:(1146表不存在)

时间:2018-09-13 09:22:13

标签: python django django-models

我在python django中进行迁移时遇到问题。当我尝试执行python manage.py makemigrations或进行迁移时,会出现此错误。

  

回溯(最近通话最近):文件   “ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ db \ backends \ utils.py”,   第65行,执行       返回self.cursor.execute(sql,params)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ db \ backends \ mysql \ base.py”,   第101行,在执行       返回self.cursor.execute(query,args)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ MySQLdb \ cursors.py”,   第250行,执行中       self.errorhandler(自我,排除,值)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ MySQLdb \ connections.py”,   第50行,在defaultErrorhandler中       引发错误值文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ MySQLdb \ cursors.py”,   第247行,执行       res = self._query(查询)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ MySQLdb \ cursors.py”,   _query中的第412行       rowcount = self._do_query(q)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ MySQLdb \ cursors.py”,   _do_query中的375行       db.query(q)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ MySQLdb \ connections.py”,   第276行,在查询中       _mysql.connection.query(自己,查询)   _mysql_exceptions.ProgrammingError:(1146,“表'rc2.eduuser_role'不存在”)

     

上述异常是以下异常的直接原因:

     

回溯(最近一次通话最后一次):文件“ manage.py”,第17行,在          execute_from_command_line(sys.argv)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ core \ management__init __。py”,   第363行,在execute_from_command_line中       utility.execute()文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ core \ management__init __。py”,   第355行,执行       self.fetch_command(subcommand).run_from_argv(self.argv)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ core \ management \ base.py”,   第283行,在run_from_argv中       self.execute(* args,** cmd_options)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ core \ management \ base.py”,   执行中的第327行       self.check()文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ core \ management \ base.py”,   359行,检查中       include_deployment_checks = include_deployment_checks,文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ core \ management \ base.py”,   _run_checks中的第346行       返回checks.run_checks(** kwargs)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ core \ checks \ registry.py”,   第81行,在run_checks中       new_errors =检查(app_configs = app_configs)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ core \ checks \ urls.py”,   第16行,在check_url_config中       返回check_resolver(resolver)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ core \ checks \ urls.py”,   第26行,在check_resolver中       返回check_method()文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ urls \ resolvers.py”,   254行,检查中       self.url_patterns中的模式:文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ utils \ functional.py”,   第35行,获取       res =实例。 dict [self.name] = self.func(instance)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ urls \ resolvers.py”,   第405行,采用url_patterns       模式= getattr(self.urlconf_module,“ urlpatterns”,self.urlconf_module)文件   “ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ utils \ functional.py”,   第35行,获取       res =实例。 dict [self.name] = self.func(instance)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ urls \ resolvers.py”,   第398行,在urlconf_module中       返回import_module(self.urlconf_name)文件“ C:\ Users \ Tim \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ importlib__init __。py”,   import_module中的第126行       在_gcd_import文件中,返回_bootstrap._gcd_import(name [level:],程序包,级别)文件“”,第994行   _find_and_load文件中的“”行971   “”,第955行,在_find_and_load_unlocked中   _load_unlocked中的文件“”,行665,
  在第678行中输入文件“”   exec_module文件“”,第219行,在   _call_with_frames_removed文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ ResourceCenter \ urls.py”,   第24行,在       url(r'^ eduuser /',include(“ eduuser.urls”,namespace =“ eduuser”)),文件   “ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ conf \ urls__init __。py”,   第50行,包括       urlconf_module = import_module(urlconf_module)文件“ C:\ Users \ Tim \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ importlib__init __。py”,   import_module中的第126行       在_gcd_import文件中,返回_bootstrap._gcd_import(name [level:],程序包,级别)文件“”,第994行   _find_and_load文件中的“”行971   “”,第955行,在_find_and_load_unlocked中   _load_unlocked中的文件“”,行665,
  在第678行中输入文件“”   exec_module文件“”,第219行,在   _call_with_frames_removed文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ eduuser \ urls.py”,   第2行       从eduuser导入视图文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ eduuser \ views.py”,   第7行       从eduuser.forms导入ForgotEmailForm文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ eduuser \ forms.py”,   第107行,在       class ImportUserForm(forms.Form):文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ eduuser \ forms.py”,   ImportUserForm中的第123行       role_choices =元组([[(u'',“Auswählen”)] +列表(Role.objects.all()]中r的列表[[(r.id,str(r))))   “ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ db \ models \ query.py”,   第250行,位于 it       self._fetch_all()文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ db \ models \ query.py”,   _fetch_all中的第1118行       self._result_cache = list(self._iterable_class(s​​elf))文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ db \ models \ query.py”,   第53行,位于 iter       结果= editor.execute_sql(chunked_fetch = self.chunked_fetch)文件   “ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ db \ models \ sql \ compiler.py”,   第886行,在execute_sql中       引发original_exception文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ db \ models \ sql \ compiler.py”,   第876行,在execute_sql中       cursor.execute(sql,params)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ db \ backends \ utils.py”,   第80行,执行       返回超级(CursorDebugWrapper,自身)。执行(sql,params)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ db \ backends \ utils.py”,   第65行,执行       返回self.cursor.execute(sql,params)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ db \ utils.py”,   第94行,位于退出       six.reraise(dj_exc_type,dj_exc_value,traceback)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ utils \ six.py”,   685号线       提高价值。with_traceback(tb)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ db \ backends \ utils.py”,   第65行,执行       返回self.cursor.execute(sql,params)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ django \ db \ backends \ mysql \ base.py”,   第101行,在执行       返回self.cursor.execute(query,args)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ MySQLdb \ cursors.py”,   第250行,执行中       self.errorhandler(自我,排除,值)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ MySQLdb \ connections.py”,   第50行,在defaultErrorhandler中       引发错误值文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ MySQLdb \ cursors.py”,   第247行,执行       res = self._query(查询)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ MySQLdb \ cursors.py”,   _query中的第412行       rowcount = self._do_query(q)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ MySQLdb \ cursors.py”,   _do_query中的375行       db.query(q)文件“ C:\ Source \ Git \ Suisseedu \ resourcecenter2 \ ResourceCenter \ ResourceCenter \ env \ lib \ site-packages \ MySQLdb \ connections.py”,   第276行,在查询中       _mysql.connection.query(self,query)django.db.utils.ProgrammingError:(1146,“表'rc2.eduuser_role'   不存在”)

代码:

from django.db import models
from django.utils.translation import gettext_lazy as _
from settings import base
from eduuser.managers import EduUserManager
from phonenumber_field.modelfields import PhoneNumberField
# ** AUTH USER **
from django.contrib.auth.models import (
    AbstractBaseUser,PermissionsMixin
)

# ------------
# ROLE
# ------------
class Role(models.Model):
    name = models.CharField(
        _("Role Name"),
        max_length=100,
        unique=True,
    )
    def __str__(self):
        return self.name

# -------------
# EduUser
# -------------
class EduUser(AbstractBaseUser,PermissionsMixin):

    objects = EduUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    first_name = models.CharField(
        _("First Name"),
        default="",
        blank=False,       
        max_length=255)

    last_name = models.CharField(
        _("Last Name"),
        default = "",
        blank=False,
        max_length=255)

    eduId = models.CharField(
        _("EduId"),
        blank=False,
        unique=True,
        db_index=True,
        error_messages={
            'unique': _("A user with that eduId already exists."),
        },
        max_length=255)

    age = models.IntegerField(
        _("Age"),
        blank=True,
        null=True,
    )

    GENDER_CHOICES = (
        ('N', 'None'),
        ('M', 'Male'),
        ('F', 'Female'),
    )

    gender = models.CharField(
        _("Gender"),
        max_length=1,
        choices=GENDER_CHOICES)

    email = models.EmailField(
        _("Email"),
        max_length=255,
        unique=True,
        error_messages={
            'unique': _("A user with that email address already exists."),
        },
    )

    verify_key = models.IntegerField(blank=True, null=True)
    verify_key_expires = models.DateTimeField(blank=True, null=True)
    email_verify = models.BooleanField(default=False)

    password_forgot_key = models.CharField(
        _("Password_Forgot_Key"),
        blank=True,
        unique=True,
        max_length=255)
    password_forgot_key_expires = models.DateTimeField(blank=True, null=True)

    health_info = models.TextField(
        _("Health Informations"),
        default="No Special Informations")
    special_info = models.TextField(
        _("Special Informations"),
        default="No Special Informations")
    resume_info = models.TextField(
        _("Biography"),
        default="No Bio set")

    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_first_steps = models.BooleanField(default=True)

    date_of_birth = models.DateField(default="2000-01-01")
    school_start_date = models.DateField(default="2000-01-01")

    eduuser_read = models.ManyToManyField(
        base.AUTH_USER_MODEL,
        related_name="eduuser_read_set",
        blank=True,
    )

    eduuser_write = models.ManyToManyField(
        base.AUTH_USER_MODEL,     
        related_name="eduuser_write_set",
        blank=True,
    )

    role = models.ManyToManyField(
        "Role",
        _("Role"),
        blank=False,
        db_index=True,
    )

    address = models.ForeignKey("country.Address",
        blank=True,
        null=True,
        default=None,
    )

    phone_number = PhoneNumberField(blank=True)

    def get_full_name(self):
        # The user is identified by their email address
        return "" + self.first_name + " " + self.last_name 
    def get_short_name(self):
        # The user is identified by their email address
        return self.email
    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True
    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True
    def is_staff(self):
        "Is the user a member of staff?"
        # Simplest possible answer: All admins are staff
        return self.is_admin
    def __str__(self):
        if self.first_name and self.last_name:
            return "" + self.first_name + " " + self.last_name
        else:
            return "" + self.email

# -------------
# Import Maching Table
# -------------

class ImportMatch(models.Model):

    role = models.OneToOneField(
        "Role",
        blank=False,
        db_index=True,
        related_name="role_host_set"
    )

    role_allowed = models.ManyToManyField(
        "Role",
        blank=False,
        db_index=True,
        related_name="allowed_role_set"
    )

    def __str__(self):
        return "" + str(self.role) + " Import Allowed Table"

# -------------
# Import Role Mapper
# -------------
class RoleMapper(models.Model):

    role = models.OneToOneField(
        "Role",
        blank=False,
        db_index=True,
    )

    role_map = models.TextField(
        _("Trennen mit Komma"),
        blank=False,
    )

    def __str__(self):
        return "" + str(self.role) + " Mapper"

有人可以通过这个问题帮助我吗? Python版本:3.6.6 Django版本:1.11.3

编辑:

role_choices = tuple([(u'', "Auswählen")] + list([(r.id, str(r)) for r in Role.objects.all()]))
role = forms.ChoiceField(widget=forms.Select, choices=role_choices)

1 个答案:

答案 0 :(得分:2)

对于从模型中进行选择的表单域,您应该始终将ModelChoiceField与查询集一起使用。

role = forms.ModelChoiceField(queryset=Role.objects.all(), empty_label="Auswählen")

在此处使用静态选择,不仅会给您带来遇到的问题,而且还会在添加或编辑Role对象时阻止选择的更新。使用ModelChoiceField可以在适当的时候进行查询,以确保查询始终是最新的。