开发者

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

开发者 https://www.devze.com 2022-12-19 08:53 出处:网络
I read this page: http://www.djangoproject.com/documentation/mode开发者_开发百科ls/custom_pk/, and the example doesn\'t list unique=True. I\'m wondering if there is a compelling reason for them to lea

I read this page: http://www.djangoproject.com/documentation/mode开发者_开发百科ls/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.

0

精彩评论

暂无评论...
验证码 换一张
取 消