我正在编写一个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基本上可以模拟组合键,并允许多个字段具有主键功能。我在正确的轨道上吗?
qq_遁去的一_1
相关分类