简体   繁体   中英

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?

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM