Django 或类似的复合主键 [英] Django or similar for composite primary keys

查看:25
本文介绍了Django 或类似的复合主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的工程公司编写一个 Web 应用程序(警告:我只是业余爱好的程序员)并计划使用 Django,直到我遇到这个障碍.我想使用的模型自然有多列主键.根据 http://code.djangoproject.com/ticket/373,我不能使用 Django,至少不是发布版本.任何人都可以帮助我解决方法,无论是通过另一个 Web 框架(请仅基于 Python)还是建议对模型进行更改,以便它可以与 Django 的限制一起使用?我真的希望是后者,因为我希望借此机会学习 Django.

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.

示例:表一有 part_number 和 part_revision 作为两个字段,应该包含一个主键.一个 P/N 可以存在于多个修订版中,但 P/N + rev 是唯一的.

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.

表二以part_number、part_revision和dimension_number为主键.特定转速下的 P/N 可以有多个维度,但是,每个维度都是唯一的.另外,在这种情况下,P/N + rev 应该是表一的外键.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆