我正在编写一个 Django 应用程序,它使用现有的遗留数据以及重新映射表和关系。我得到了一个表,其中包含表之间的主外键关系,但我无法理解如何使其适应 Django 模型。
例如,一个表country_metrics
被定义为
+-------------+---------------+------+-----+---------+-------+
| 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 | |
+-------------+---------------+------+-----+---------+-------+
我得到了一个关系映射
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
它已在课堂上建模
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")
根据我的阅读,unique_together
基本上模拟了一个复合键,并允许多个字段具有主键功能。我在正确的轨道上吗?