Django model with 2 foreign keys from the same table

Josh picture Josh · Feb 24, 2009 · Viewed 60.2k times · Source

I wanted a Django model with 2 foreign keys from the same table. It's an event table which has 2 columns for employees: the 'actor' and the 'receiver'. But I get this error:

Error: One or more models did not validate: tasks.task: Intermediary model TaskEvent has more than one foreign key to Employee, which is ambiguous and is not permitted.

Is there a better way to model this? Thanks

I think I'm going to add a TaskEvent_to_Employee table. There will be two records in it, one for each of the two employees related to each TaskEvent. Anyone know an easier workaround?

Answer

Technical Bard picture Technical Bard · Feb 24, 2009

I haven't done this yet, but I used inspectdb to generate the models.py file from an existing DB that does exactly that - this is what inspectdb threw back, so it should work:

creator = models.ForeignKey(Users, null=True, related_name='creator')
assignee = models.ForeignKey(Users, null=True, related_name='assignee')

Hope that works for you - if it doesn't I am going to have a problem too.