django.db.utils.ProgrammingError: there is no unique constraint matching given keys for referenced table

Viewed 12

I am having a problem with Django 2.2.7 and postgresql 12 using the command "python manage.py migrate".

When I execute it, the process fails with the following error: django.db.utils.ProgrammingError: there is no unique constraint matching given keys for referenced table "clients_clients"

I understand that this error indicates that when a field is used as a foreing key in another table, this field must be unique.

My model clients in Django is:

class Clients(models.Model):
    name = models.CharField(max_length=60, unique=True)
    document_num = models.CharField(max_length=15)
    phone = models.CharField(max_length=15, blank=True)
    email = models.EmailField(max_length=30, blank=True)
    instagram = models.CharField(max_length=30, blank=True)
    address = models.TextField(max_length=100, blank=True)

The model with the foreing key to the field "name" of clients_clients is:

class Budgets(models.Model):
    date = models.DateField(error_messages={'null': "You must set a date"})
    title = models.CharField(max_length=50, unique=True)
    client = models.ForeignKey(Clients, null=True, on_delete=models.SET_NULL, to_field='name')
    price = models.DecimalField(default=0, decimal_places=2, max_digits=10)
    observations = models.TextField(max_length=200, blank=True)

As is shown above, the field "name" in model "Clients" is set as unique=True. But in spite of that, the error mentioned is shown.

Anyone can help me to understand why?

0 Answers
Related