Many-to-many intermediary tables can't have multiple foreign keys to source/target models
|Reported by:||coda||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Severity:||Normal||Keywords:||m2m intermediary validation|
|Cc:||mysliceof314, danny.adair@…||Triage Stage:||Accepted|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
The following schema:
from django.db import models class Person(models.Model): name = models.CharField(max_length=100) vacations = models.ManyToManyField('Location', through='Vacation', blank=True) class Location(models.Model): city = models.CharField(max_length=100) country = models.CharField(max_length=100) class Vacation(models.Model): person = models.ForeignKey(Person) location = models.ForeignKey(Location) date = models.DateField() travel_agent = models.ForeignKey(Person, related_name='booked_vacations')
doesn't pass validation because "Intermediary model Vacation has more than one foreign key to Person, which is ambiguous and is not permitted."
It looks to me like the only reason for this error is because django doesn't have a way to specify which foreign keys on the intermediary model are relevant (or that there is no default behavior specified).
There's obviously more than one way around this, and probably someone can come up with a nicer example schema than I did. The simplest thing would be to get rid of that validation check, which (i think) would cause django to use the first key found for each model.
Change History (14)
comment:1 Changed 7 years ago by russellm
- Component changed from Database wrapper to Core framework
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Accepted
comment:7 Changed 5 years ago by julien
- Component changed from Core framework to Database layer (models, ORM)