Django或类似的复合主键 [英] Django or similar for composite primary keys
问题描述
示例:
表一有part_number和part_revision为应包括主键的两个字段。 P / N可以存在多个修订版本,但P / N + rev是唯一的。
表二以part_number,part_revision和dimension_number为主键。特定转速的P / N可以有多个维度,但是每个维度都是独一无二的。此外,在这种情况下,P / N + rev应该是表1的ForeignKey。
一个代理键(一个自动增量列)作为主键列,并在您的域复合键上放置一个唯一的索引。
外键然后将引用代理主键列。
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.
A work around is to create a surrogate key (an auto increment column) as the primary key column and place a unique index on your domain composite key.
Foreign keys will then refer to the surrogate primary key column.
这篇关于Django或类似的复合主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!