2

我想将 Django 排除约束与 ManyToManyField 一起使用。不幸的是,到目前为止,我的努力都是徒劳的。这是我的约会模式:

从 django.contrib.postgres.constraints 导入 ExclusionConstraint 从 django.contrib.postgres.fields 导入 DateTimeRangeField, RangeOperators

class Appointment:
    patients = models.ManyToManyField(Patient, related_name='appointments' , blank=True )     
    datetimerange = DateTimeRangeField(null=False, blank = False )
    doctor = models.ForeignKey(Doctor, related_name='doctor_appointments') 

    class Meta: 
        constraints = [ 
            ExclusionConstraint(
                name='unique_doctor',
                expressions=[
                    ('datetimerange', RangeOperators.OVERLAPS),
                    ('doctor ', RangeOperators.EQUAL),

                ], 
            ), 
            ExclusionConstraint(
                name='unique_patients',
                expressions=[
                    ('datetimerange', RangeOperators.OVERLAPS),
                    ('patients', RangeOperators.CONTAINED_BY)
                
                ],
                condition= Q(is_archived=False) & Q(is_cancelled=False)                
        ) 
        ]

不幸的是,这不起作用。引用 Doctor 的第一个约束完美运行,但第二个约束在迁移期间给了我这个错误:

return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedColumn: column "patient_id" named in key does not exist

    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "patient_id" named in key does not exist

这让我困扰了很长一段时间。任何帮助表示赞赏。

4

0 回答 0