Python

Python

By DeepSource

Primary key is not unique PTC-W0902

Bug risk
Autofix

Values in field which is used as primary key should be unique.

A primary key is used to uniquely identify each record in the Model's database table.

A non-unique primary key allows the same value to be used for multiple records. Therefore, multiple objects can exist with the same primary key, which might not be something that was intended.

The kwarg primary_key=True denotes that the field is a primary key for the model. It implicitly means null=False and unique=True.

Providing unique=False is not recommended.

Bad practice

class MyModel(models.Model):
    my_field = models.CharField(
        primary_key=True,
        unique=False
    )

Recommended

class MyModel(models.Model):
    my_field = models.CharField(primary_key=True)