首页 > 解决方案 > Django 3.1:OperationalError - 没有这样的列/表

问题描述

我整天都在解决这个问题。我的 models.py 中的代码如下:

from django.db import models
from django.contrib.auth.models import User
from users.models import TeacherProfile, StudentProfile

class Course(models.Model):
    name = models.CharField(max_length=100)
    desc = models.TextField()
    short_code = models.CharField(max_length=5)
    teacher = models.ForeignKey(TeacherProfile, null=True, on_delete=models.SET_NULL)
    students = models.ManyToManyField(StudentProfile)

    def __str__(self):
        return self.name

class Partition(models.Model):
    name = models.CharField(max_length=20)
    chunk = models.FloatField()
    result = models.FloatField(blank=True)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    
    def __str__(self):
        return self.name
    
class Activity(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    file = models.FileField(blank=True)
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    parent = models.ForeignKey(Partition, on_delete=models.CASCADE)

    def __str__(self):
        return self.title
    
class Grade(models.Model):
    grade = models.FloatField()
    remarks = models.CharField(max_length=200, blank=True)
    activity = models.ForeignKey(Activity, on_delete=models.CASCADE)
    student = models.ForeignKey(StudentProfile, on_delete=models.CASCADE)

我已经运行了以下命令:

python manage.py makemigrations
python manage.py migrate

我什至根据我对与此相关的其他 StackOverFlow 问题的研究编写了以下命令:

python manage.py migrate --run-syncdb

只有课程表有效。Activity 和 Grade 表收到 OperationalError - No such Column 和 Partition Table 收到 OperationalError - No such Table。

活动

OperationalError at /admin/course/activity/
no such column: course_activity.parent_id
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/course/activity/
Django Version: 3.1
Exception Type: OperationalError
Exception Value:    
no such column: course_activity.parent_id
Exception Location: C:\Users\hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\sqlite3\base.py, line 413, in execute
Python Executable:  C:\Users\hp\AppData\Local\Programs\Python\Python38-32\python.exe
Python Version: 3.8.5
Python Path:    
['C:\\Users\\hp\\Desktop\\TechKnow\\YowBro',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\lib',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages']
Server time:    Fri, 21 Aug 2020 09:47:26 +0000

年级

OperationalError at /admin/course/grade/
no such column: course_grade.remarks
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/course/grade/
Django Version: 3.1
Exception Type: OperationalError
Exception Value:    
no such column: course_grade.remarks
Exception Location: C:\Users\hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\sqlite3\base.py, line 413, in execute
Python Executable:  C:\Users\hp\AppData\Local\Programs\Python\Python38-32\python.exe
Python Version: 3.8.5
Python Path:    
['C:\\Users\\hp\\Desktop\\TechKnow\\YowBro',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\lib',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages']
Server time:    Fri, 21 Aug 2020 09:47:28 +0000

分割

OperationalError at /admin/course/partition/
no such table: course_partition
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/course/partition/
Django Version: 3.1
Exception Type: OperationalError
Exception Value:    
no such table: course_partition
Exception Location: C:\Users\hp\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\backends\sqlite3\base.py, line 413, in execute
Python Executable:  C:\Users\hp\AppData\Local\Programs\Python\Python38-32\python.exe
Python Version: 3.8.5
Python Path:    
['C:\\Users\\hp\\Desktop\\TechKnow\\YowBro',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\python38.zip',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\DLLs',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\lib',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32',
 'C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages']
Server time:    Fri, 21 Aug 2020 09:47:29 +0000

我做错了什么?

更新:我尝试将默认值放入表中。可悲的是,它没有用。请帮忙。

更新#2:我跑了python manage.py sqlmigrate course 0001。与在实际站点中显示的错误相反,返回的 SQL 命令如下所示(我突出显示了用于创建表 Partition 的 SQL 命令以强调):在此处输入图像描述

假设:可能错误在migration命令中。我从我的 SQLite 数据库浏览器中手动删除了这些表。然后,我尝试运行makemigrationsandmigrations命令。尽管它迁移了 SQL 代码,但它根本不会创建任何东西。我们如何解决这个问题?

我解决了这个问题!在下面检查我的答案。

标签: pythonpython-3.xdjangosqlite

解决方案


我解决了这个问题。

感谢 kerasbaz,我在 Django 设置中检查了我的数据库配置。我认为我应该注释掉 SQLite 的配置并插入 PostgreSQL。

所以我首先要做的是通过我的 pgAdmin 创建一个新的数据库。现在,您需要安装 pgAdmin 和 psycopg2 ( pip install psycopg2)。接下来,我在项目设置中编辑了数据库设置。

这是 Django 文档中的一个示例:

 DATABASES = {
     'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'mydatabase',
            'USER': 'mydatabaseuser',
            'PASSWORD': 'mypassword',
            'HOST': '127.0.0.1',
            'PORT': '5432',
       }
}

最后,我跑了makemigrationsmigrate发号施令。瞧!我解决了我已经解决了五个小时的问题。


推荐阅读