python - 如何修复: Django error: no such table: main.classroom_student__old

标签 python django

我正在关注this tutorial用于学习如何创建具有多种用户类型(本例中为教师和学生)的 Django (v2.0.1) 应用程序。我从 Github Repository 克隆了相关代码,迁移预制迁移并使用以下命令在本地主机上运行站点:

python3 manage.py migrate
python3 manage.py runserver

该网站几乎完美运行(教师注册、登录、测验创建和学生登录均按顺序进行)。但是,学生注册失败并出现以下错误:

OperationalError at /accounts/signup/student/
no such table: main.classroom_student__old

回溯最终指向文件

django_school/classroom/forms.py, line 39:
student.interests.add(*self.cleaned_data.get('interests'))

该行来自 forms.py 文件中以下类的定义:

class StudentSignUpForm(UserCreationForm):
    interests = forms.ModelMultipleChoiceField(
        queryset=Subject.objects.all(),
        widget=forms.CheckboxSelectMultiple,
        required=True
    )

    class Meta(UserCreationForm.Meta):
        model = User

    @transaction.atomic
    def save(self):
        user = super().save(commit=False)
        user.is_student = True
        user.save()
        student = Student.objects.create(user=user)
        student.interests.add(*self.cleaned_data.get('interests'))
        return user

我尝试过的:
根据此网站上许多类似问题的答案,我认为这是一个迁移问题,因此我尝试运行:

python manage.py migrate --run-syncdb

Operations to perform:
  Synchronize unmigrated apps: crispy_forms, humanize, messages, staticfiles
  Apply all migrations: auth, classroom, contenttypes, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
Running migrations:
  No migrations to apply.

但是错误仍然存​​在。然后,我删除了 db.sqlite3 以及与应用教室关联的所有迁移文件。然后我运行了python3 manage.py makemigrations,然后运行了python manage.py migrate --run-syncdb,再次无济于事。

这让我认为这是代码向“学生”用户对象添加“兴趣”的方式的问题。事实上,注释掉有问题的行会阻止错误并创建新的学生用户,但是这有一个明显的问题,即学生没有存储兴趣。

运行python manage.py sqlmigrate Classroom 0001显示:

...
-- Add field quizzes to student
--
ALTER TABLE "classroom_student" RENAME TO "classroom_student__old";
CREATE TABLE "classroom_student" ("user_id" integer NOT NULL PRIMARY       KEY REFERENCES "classroom_user" ("id") DEFERRABLE INITIALLY DEFERRED);
INSERT INTO "classroom_student" ("user_id") SELECT "user_id" FROM     "classroom_student__old";
DROP TABLE "classroom_student__old";
COMMIT;

因此,当另一个属性“quizzes”添加到学生用户对象时,相关数据库 (classroom_student__old) 就会被创建和删除。这会造成问题吗?

最佳答案

使您列出的教程成功运行的另一个解决方案是,在运行“pip install -rrequirements.txt”命令之前将requirements.txt文件的django版本更改为2.2.7,而不是降级到sqlite 3.24.0。

因此,该项目的requirements.txt 文件将是:

Django==2.2.7

django-crispy-forms==1.7.0

pytz==2017.3

关于python - 如何修复: Django error: no such table: main.classroom_student__old,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53906479/

相关文章:

python - 如何强制 pip 安装最新版本的 django?

python - 如何在 PyCharm 项目 View 中移动项目文件夹?

django - 动态(干净地)向 Django 表单添加字段

django - 如何使用 Django HttpResponseRedirect 提供规范 URL?

python - 从2D numpy数组中删除特定行值的数组的快速方法

python - Django:如何从相关管理器访问原始实例?

python - 适用于 Python 2.7 的 Visual Microsoft Visual C++ 编译器与 MinGW

python - 如何使用循环从 Pandas 数据框中获取值?

python - 替代字节数组处理瓶颈的高速替代方案

python - 苹果操作系统 X : Trouble with Local Django + PyDev install working with remote MySQL