2

I'm trying to connect Django with PostgreSQL, so far everything was going good, I created a model for female clients

from django.db import models
from django.db.models import CheckConstraint, Q

class fml_tbl(models.Model):
     fml_id = models.CharField(primary_key = True, max_length = 10)
     first_name = models.CharField(max_length = 20, null = False)
     last_name = models.CharField(max_length = 20)
     age = models.PositiveIntegerField(unique = True)
     qualification = models.CharField(max_length = 50)
     profession = models.CharField(max_length = 50)
     officer = models.ForeignKey("officer", on_delete=models.CASCADE, null = False)

     class Meta:
          constraints = [
               models.CheckConstraint(check=Q(age__gte=18), name='age_gte_18')
          ]

I added a check constraint for checking the age of the client, when I migrate, I get the error...

django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
?: (models.E032) constraint name 'age_gte_18' is not unique amongst models: client.fml_tbl, client.ml_tbl.

System check identified 1 issue (0 silenced).

there is also a male clients table same as this one, i'm just trying to practice django model constraints because postgresql also uses constraints, but i can't understand what to do here. I'm using Django 3.0 and Python 3.7

I searched for different answers on stackoverflow and other places but i can't find anything, I used the django documentation and this site for understanding constraints but so far this is all what the documentation says about my error, and this is the only SO answer I find on CheckConstraints, but this doesn't solve my problem.

Kindly help me out, and explain it as simply as possible because i'm a newbie. I'll really appreciate the help!

2 Answers2

3

You're trying to define two different indexes with the same name, age_gte_18. The system check is telling you not to do that. That limitation doesn't seem to be discussed in the Django documentation, but presumably is a limitation of one or more of the supported databases (see this answer, for example).

The solution is simple—give each index a unique name (or simply leave the name out).

Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102
0

You can ignore the system check with the Django setting SILENCED_SYSTEM_CHECKS = ["models.E032"]

Here is the reference for the SILENCED_SYSTEM_CHECKS setting.

Only ignore system checks if you know what you are doing. For example I needed to ignore this check when connecting Django to an existing MySQL database with several indexes on different tables using the same name. In the version of MySQL I was using this is not an issue whereas in other database backends it is.

rrauenza
  • 6,285
  • 4
  • 32
  • 57
kjpc-tech
  • 531
  • 1
  • 5
  • 8