In my Django project the in one of the model I need to use two UniqueConstraint instances. But when I add that and do the migration after running makemigrations it gives an error in the terminal.
Model class:
class MyDays(models.Model):
class Meta:
verbose_name = "My Day"
verbose_name_plural = "My Days"
constraints = [
models.UniqueConstraint(fields=['userid', 'date', 'from'], condition=Q(status=1), name='user_date_from_a'),
models.UniqueConstraint(fields=['userid', 'date', 'to'], condition=Q(status=1), name='user_date_to_b')
]
id = models.BigAutoField(primary_key=True)
userid = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="User")
date = models.DateField(auto_now_add=False, editable=True, validators=[validate_date])
from = models.TimeField(auto_now_add=False, editable=True)
to = models.TimeField(auto_now_add=False, editable=True)
status = models.ForeignKey(Status, on_delete=models.CASCADE, verbose_name="Status", default=1)
When I run python3 manage.py migrate, it gives the following error:
django.core.exceptions.FieldError: Joined field references are not permitted in this query
I need to have unique records only if the status is 1 along with the other 3 field combination. What am I doing wrong? How can I fix this error?