Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django or similar for composite primary keys

I am writing a web application for my engineering company (warning: I am a programmer only by hobby) and was planning on using Django until I hit this snag. The models I want to use naturally have multi-column primary keys. Per http://code.djangoproject.com/ticket/373, I can't use Django, at least not a released version. Can anyone help me with a workaround, whether it be via another web framework (Python-based only, please) or by suggesting changes to the model so it will work with Django's limitations? I am really hoping for the latter, as I was hoping to use this as an opportunity to learn Django.

Example: Table one has part_number and part_revision as two fields that should comprise a primary key. A P/N can exist at multiple revisions, but P/N + rev are unique.

Table two has part_number, part_revision and dimension_number as its primary key. A P/N at a specific rev can have a number of dimensions, however, each is unique. Also, in this case, P/N + rev should be a ForeignKey of Table one.

like image 748
Matthew DeNardo Avatar asked Oct 26 '09 11:10

Matthew DeNardo


People also ask

Does Django support composite primary key?

Django does not support composite primary keys.

Does Django support multiple primary keys?

Do Django models support multiple-column primary keys? ¶ No. Only single-column primary keys are supported.

Should I use composite primary keys?

Composite keys in SQL prove to be useful in those cases where you have a requirement of keys that can uniquely identify records for better search purposes, but you do not possess any single unique column. In such cases, you must combine multiple columns to create a unique key.

Is composite key and composite primary key same?

Now a composite key is also a primary key, but the difference is that it is made by the combination of more than one column to identify the particular row in the table.


1 Answers

Why not add a normal primary key, and then specify that part_number and part_revision as unique_together?

This essentially is the Djangoish (Djangonic?) way of doing what Mitch Wheat said.

like image 152
Dominic Rodger Avatar answered Sep 18 '22 22:09

Dominic Rodger