Thursday, October 6, 2011

how to access a foreignKey with multiple constraints

I have a legacy Oracle database which I'm trying to model. I used
inspectdb which did a good job. However, a few of my tables contain
two fields which combine to form a foreign key constraint. In other
words the legacy database was created like this:

CREATE TABLE "MY_TABLE"
("TRACKING_ID" NUMBER NOT NULL ENABLE,
"LAST_NAME" VARCHAR(50 BYTE) NOT NULL ENABLE,
"FIRST_NAME" VARCHAR(50 BYTE) NOT NULL ENABLE
CONSTRAINT 'EMP_REF_FK" FOREIGN KEY ("LAST NAME", "FIRST NAME")
REFERENCES "EMPLOYEE_REF"("LAST_NAME, "FIRST_NAME") ENABLE

The EMPLOYEE_REF table has a primary key and the last name and first
name fields.

The models I'm using is:

class EmployeeRef(models.Model):
employee_ref_id = models.IntegerField(primary_key=True)
last_name = models.CharField(max_length=50)
first_name = models.CharField(max_length=50)

class MyTable(models.Model):
hour_tracking_id = models.DecimalField(max_digits=10,
decimal_places=0, primary_key=True)
last_name = models.ForeignKey(EmployeeRef, db_column='last_name',
related_name='employee.last_name')
first_name = models.ForeignKey(EmployeeRef,
db_column='first_name', related_name='employee.first_name')

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment