我整天都在解决这个问题。我在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
仅课程表有效。活动和成绩表收到OperationalError-没有这样的列,而分区表得到了OperationalError-没有这样的表。
活动
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命令如下所示(我突出显示了用于创建表分区的SQL命令以进行强调):
假设::错误可能出在migration
命令中。我从用于SQLite的数据库浏览器中手动删除了表。然后,我尝试运行makemigrations
和migrations
命令。尽管它迁移了SQL代码,但它根本不会创建任何东西。我们该如何解决?
我已解决问题!在下面检查我的答案。
答案 0 :(得分:1)
您可以尝试在模型中添加相关名称,例如
$(table).on('length.dt')
答案 1 :(得分:1)
我已解决问题。
多亏了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',
}
}
最后,我运行了makemigrations
和migrate
命令。瞧!我解决了我已经解决了五个小时的问题。