A non-unique primary key allows the same value to be used for multiple records thus multiple results could be returned for Model.objects.get(pk=1)
.
A primary key is used to uniquely identify each record in the Model's database table.
primary_key=True denotes the field is the primary key for the model.
primary_key=True implies null=False and unique=True.
So in practice, do this
class SomeModel(models.Model):
some_field = models.CharField(primary_key=True)
Instead of this
class SomeModel(models.Model):
some_field = models.CharField(
primary_key=True,
unique=False
)
Django Doctor will run this check by default. No configuration is needed but the check can be turned on/off using check code non-unique-primary
in your pyproject.toml file.