简体   繁体   中英

Django models and primary-foreign key relationships

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_together basically 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 KEY s 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 , ie a key on a single, unique, field.

See also this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM