Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Using custom primary key, should I specifiy unique=True?

I read this page: http://www.djangoproject.com/documentation/models/custom_pk/, and the example doesn't list unique=True. I'm wondering if there is a compelling reason for them to leave it out, or if there is some reason I should include it. My assumption is that specifying primary_key=True does this automatically though. Any thoughts?

like image 866
orokusaki Avatar asked Jan 23 '23 15:01

orokusaki


2 Answers

http://docs.djangoproject.com/en/2.1/ref/models/fields/#primary-key

Your assumption is correct, primary_key=True implies unique=True.

like image 195
Josh Wright Avatar answered Jan 25 '23 15:01

Josh Wright


Josh Wright's answer is right on, but I'd also recommend reading a text about relational databases. By definition, a primary key must be unique, so it would be a bug if Django allowed a primary key to be non-unique. I highly recommend "Database Design for Mere Mortals: A Hands-On Guide to Relational Database Design (2nd Edition)", by Michael Hernandez. It is full of valuable practical advice.

like image 34
Luciano Ramalho Avatar answered Jan 25 '23 16:01

Luciano Ramalho