I have a legacy database with the following tables:
person
person_id (PK) | first_name | last_name
1 | John | Doe
2 | David | Bentley
phonenumbers
person_id (FK,PK) | phone_number (PK) | area_code (PK)
1 | 758-4551 | 909
1 | 763-3445 | 909
2 | 634-0011 | 637
Every person can have zero or more phone numbers, which is a multivalued attribute of the person entity.
I've tried using the Django's inspectdb command which generated the following models.py:
class Person(models.Model):
person_id = models.BigIntegerField(primary_key=True)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
class Meta:
managed = False
db_table = 'person'
class PhoneNumbers(models.Model):
person = models.ForeignKey(Person, models.DO_NOTHING)
phone_number = models.CharField(max_length=15)
area_code = models.CharField(max_length=15)
class Meta:
managed = False
db_table = 'phonenumbers'
unique_together = (('person', 'phone_number', 'area_code'),)
However, when I tried to save a new instance of PhoneNumbers, Django returned the following error message:
django.db.utils.ProgrammingError: column phonenumbers.id does not exist
Apparently Django expects the phone number's table to have a surrogate key. Since the phone number's table is not an entity, it doesn't have a surrogate key in my legacy database. Notice that the phonenumbers' table primary key is a composition of all of its columns.
How can I map these tables into Django's models so that it works with my legacy database?