Version 16 (modified by Ben Finney, 10 years ago) (diff)

clarify a question

Composite Primary Key Design Notes

Collating design considerations, options and decisions here to help get this feature moving.

Design Questions

How do you specify multiple fields for the primary key?

The "neatest" solution would be to allow primary_key=True on multiple fields but this may be precluded by other design considerations.

How do you foreign key onto a table with a composite foreign key?

The primary key is a set of the columns.

What do you use for .get(pk=foo) with a composite primary key?

The primary key is a set of the columns.

There is mention of the comment framework overlapping with this.

How does Admin map a url to an object?

Possible solutions (for a key with the value { val_foo, val_bar, val_baz } :

  • /appname/modulename/val_foo,val_bar,val_baz/

How to deal with values containing commas? Should be able to be handled with some nice url escaping.

  • /appname/modulename/val_foo/val_bar/val_baz/

How to deal with identity between different orders of the same set?

The columns of a key are a set, and have no implicit order. This means the key { val_foo, val_bar, val_baz } is identical to { val_baz, val_foo, val_bar } and so on for all permutations. How should URLs be parsed to reflect this identity?

Affected Areas


On going in depth notes

Need a way of defining the primary key.

Generation of the create table SQL.

A solution for foreign keying onto a table with a composite primary key.

Many to many relationships should use composite primary keys if they are unique . . .


Model.objects.get(pk=foo) needs to be supported.

Solution: the primary key is a set of the column values.



Expect there is a large amount of work in here making sure all the model inspection and form generation works.

URL to object mapping.


Code needs to be looked at further but all the foreign keying is likely to need attention.

Relevant Discussions

Back to Top