Django 查询中的 PostgreSQL 编程错误
PostgreSQL Programming error in Django Query
我有一个查询如下,returns特定class的所有学生在特定学期(学期)和特定学期(学年)的成绩:
grades = Grade.objects.filter(term='First', student__in_class=1,session=1).order_by('-total')
然后是另一个查询,通过成绩进行注释以获得 'total' 字段的总和。
grades_ordered = grades.values('student')\
.annotate(total_mark=Sum('total')) \
.order_by('-total_mark')
起初一切正常,直到我从使用 SQLite 迁移到 postgreSQL 然后开始出现以下错误。
错误:
function sum(character varying) does not exist
LINE 1: SELECT "sms_grade"."student_id", SUM("sms_grade"."total") AS...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
编辑:
这是我的模型
class Grade(models.Model):
session = models.ForeignKey(Session, on_delete=models.CASCADE)
term = models.CharField(choices=TERM, max_length=7)
student = models.ForeignKey(Student, on_delete=models.CASCADE)
subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
fca = models.CharField(max_length=10)
sca = models.CharField(max_length=10)
exam = models.CharField(max_length=10)
total = models.CharField(max_length=3, blank=True, null=True)
grade = models.CharField(choices=GRADE, max_length=1, blank=True, null=True)
remark = models.CharField(max_length=50, blank=True, null=True)
如果您能提供任何帮助,我们将不胜感激。
谢谢
我有一个查询如下,returns特定class的所有学生在特定学期(学期)和特定学期(学年)的成绩:
grades = Grade.objects.filter(term='First', student__in_class=1,session=1).order_by('-total')
然后是另一个查询,通过成绩进行注释以获得 'total' 字段的总和。
grades_ordered = grades.values('student')\
.annotate(total_mark=Sum('total')) \
.order_by('-total_mark')
起初一切正常,直到我从使用 SQLite 迁移到 postgreSQL 然后开始出现以下错误。
错误:
function sum(character varying) does not exist
LINE 1: SELECT "sms_grade"."student_id", SUM("sms_grade"."total") AS...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
编辑: 这是我的模型
class Grade(models.Model):
session = models.ForeignKey(Session, on_delete=models.CASCADE)
term = models.CharField(choices=TERM, max_length=7)
student = models.ForeignKey(Student, on_delete=models.CASCADE)
subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
fca = models.CharField(max_length=10)
sca = models.CharField(max_length=10)
exam = models.CharField(max_length=10)
total = models.CharField(max_length=3, blank=True, null=True)
grade = models.CharField(choices=GRADE, max_length=1, blank=True, null=True)
remark = models.CharField(max_length=50, blank=True, null=True)
如果您能提供任何帮助,我们将不胜感激。
谢谢