Django check: Set string field to avoid constraint violations

If a text field declares unique=True and blank=True, then null=True must also be set to avoid unique constraint violations when saving multiple objects with blank values.

Description

According to the Django documentation, when a CharField has both unique=True and blank=True set, null=True is required to avoid unique constraint violations when saving multiple objects with blank values.

This check will alert on these cases, for example:

# SHOULD MATCH
from django.db import models
from django.db.models import Model

class ExampleModel(Model):
    # when unique=True and blank=True, required null=True is missing
    fieldTwo = models.CharField(
        unique=True,
        blank=True,
        max_length=30
    )
    # when unique=True and blank=True, required null=True is missing
    fieldAAAAA = models.TextField(
        unique=True,
        blank=True,
        max_length=30
    )

This check will not alert on these cases:

# SHOULD NOT MATCH
from django.db import models
from django.db.models import Model

class ExampleModel(Model):
    # no alerts triggered when 
    # unique=True, blank=True, null=True
    fieldThree = models.CharField(
        unique=True,
        null=True,
        blank=True,
        max_length=100
    )

References

  1. Django documentation