[Fixed]-Django: Does "primary_key=True" also mean "unique"?

24👍

Yes. Since a primary key means a value that can uniquely identify an object. In the documentation on the primary_key parameter, we see:

Field.primary_key

If True, this field is the primary key for the model.

If you don’t specify primary_key=True for any field in your model,
Django will automatically add an AutoField to hold the primary key,
so you don’t need to set primary_key=True on any of your fields
unless you want to override the default primary-key behavior. For
more, see Automatic primary key fields.

primary_key=True implies null=False and unique=True. Only one primary key is allowed on an object.

Leave a comment