开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜