Compound/Composite primary/unique key with Django

Viewed 25445

How can you create models (and thus tables) with a compound (composite) primary/unique key using Django?

3 Answers

Django does not support compound primary keys. You can create a single compound unique key with Meta.unique_together.

A composite key consists of more than one attribute to uniquely identify an entity occurrence. This differs from a compound key in that one or more of the attributes, which make up the key, are not simple keys in their own right.

For example, you have a database holding your CD collection. One of the entities is called tracks, which holds details of the tracks on a CD. This has a composite key of CD name, track number.

Related