Django模型和初级 - 外键关系 [英] Django models and primary-foreign key relationships

查看:133
本文介绍了Django模型和初级 - 外键关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Django应用程序,该应用程序使用现有的旧数据以及重新映射表和关系。我已经给了一个包含表之间的主外键关系的表,但是我很难理解如何使它适应Django模型。



例如,表 country_metrics 被定义为

  + ------- ------ + --------------- + ------ + ------ + --------- + ---- --- + 
| Field |类型|空|关键|默认|额外|
+ ------------- + --------------- + ------ + ----- + - ------- + ------- +
| ContractNum | varchar(10)| NO | | NULL | |
| fips | varchar(5)| NO | | NULL | |
| metric_id | int(10)| NO | | NULL | |
| time_id | int(10)| NO | | NULL | |
|值|十进制(12,3)| NO | | NULL | |
| eff_date |日期| NO | | NULL | |
| exp_date |日期|是| | NULL | |
+ ------------- + --------------- + ------ + ----- + - ------- + ------- +

我一直给出关系映射

  ContractNum主键,外键引用表合约
fips主键,外键引用表国家
metric_id主键,外键引用表指标
time_id主键,外键引用表时间

它被建模在类

  class ContractCountyMetrics(models.Model):
contractnum = models.CharField(max_length = 10L,db_column ='ContractNum',primary_key = true)#字段名称小写。
fips = models.CharField(max_length = 5L)
metric_id = models.IntegerField()
time_id = models.IntegerField()
value = models.DecimalField(max_digits = decimal_places = 3)
eff_date = models.DateField()
exp_date = models.DateField(null = True,blank = True)
contractKey = models.ForeignKey(Contracts)
fipsKey = models.ForeignKey(县)
metricKey = models.ForeignKey(Metrics)
timeKey = models.ForeignKey(Time)
class Meta:
db_table ='txn_contract_county_metrics'
unique_together(contractKey,fipsKey,metricKey,timeKey)

我读到, unique_together 基本上模拟一个复合键,并允许多个字段具有主键功能。我在正确的轨道上吗?

解决方案


从我读的 unique_together 基本上模拟复合
键,并允许多个字段具有主键功能。
我在正确的轨道上?


不完全。它只是将一个复合 UNIQUE KEY 添加到指定的字段中,并且在创建表时只有真正的效果,如果您使用Django访问传统表。在Django中仍然不支持复合 PRIMARY KEY (请参阅错误#373 )。



不幸的是,这可能意味着您将无法将Django与遗留表一起使用, code> PRIMARY KEY ,而不修改表以包含一个Django兼容的 PRIMARY KEY ,即单个唯一,



另请参见这个问题


I'm writing a Django app that uses existing legacy data combined with a remapping of tables and relationships. I've been given a table containing the primary-foreign key relationships between the tables, but am having trouble understanding how to adapt it to Django models.

For example, a table country_metrics is defined as

+-------------+---------------+------+-----+---------+-------+
| Field       | Type          | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| ContractNum | varchar(10)   | NO   |     | NULL    |       |
| fips        | varchar(5)    | NO   |     | NULL    |       |
| metric_id   | int(10)       | NO   |     | NULL    |       |
| time_id     | int(10)       | NO   |     | NULL    |       |
| value       | decimal(12,3) | NO   |     | NULL    |       |
| eff_date    | date          | NO   |     | NULL    |       |
| exp_date    | date          | YES  |     | NULL    |       |
+-------------+---------------+------+-----+---------+-------+

and I've been given a relationship mapping of

ContractNum primary key, foreign key references table contracts
fips        primary key, foreign key references table country
metric_id   primary key, foreign key references table metrics
time_id     primary key, foreign key references table time

Its been modeled in the class

class ContractCountyMetrics(models.Model):
  contractnum = models.CharField(max_length=10L, db_column='ContractNum', primary_key = true) # Field name made lowercase.
  fips = models.CharField(max_length=5L)
  metric_id = models.IntegerField()
  time_id = models.IntegerField()
  value = models.DecimalField(max_digits=14, decimal_places=3)
  eff_date = models.DateField()
  exp_date = models.DateField(null=True, blank=True)
  contractKey = models.ForeignKey(Contracts)
  fipsKey = models.ForeignKey(Counties)
  metricKey = models.ForeignKey(Metrics)
  timeKey = models.ForeignKey(Time)
  class Meta:
      db_table = 'txn_contract_county_metrics'
      unique_together("contractKey", "fipsKey", "metricKey", "timeKey")

From what I read, unique_togetherbasically simulates a composite key and allows more than one field to have primary key functionality. Am I on the right track?

解决方案

From what I read, unique_together basically simulates a composite key and allows more than one field to have primary key functionality. Am I on the right track?

Not quite. It just adds a composite UNIQUE KEY to the specified fields, and only really has any effect during the creation of the table, which won't apply if you're using Django to access a legacy table. There's still no support for composite PRIMARY KEYs in Django (see bug #373).

Unfortunately, this probably means that you won't be able to use Django with a legacy table which has a composite PRIMARY KEY, without modifying the table to include a Django-compatible PRIMARY KEY, i.e. a key on a single, unique, field.

See also this question.

这篇关于Django模型和初级 - 外键关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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