Opened 16 years ago
Closed 10 years ago
#8618 closed New feature (duplicate)
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: | dev |
Severity: | Normal | Keywords: | m2m intermediary validation |
Cc: | Andrew Gibson, danny.adair@… | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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 by , 16 years ago
Component: | Database wrapper → Core framework |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 16 years ago
Another option(which I'm not necessarily endorsing), would be to make through accept a tuple, the first arg is the intermediary model and the second is the name of the fkey to this model, and the third is the name of the fkey to the other model.
comment:3 by , 16 years ago
milestone: | → post-1.0 |
---|
comment:4 by , 16 years ago
Cc: | added |
---|
comment:7 by , 14 years ago
Component: | Core framework → Database layer (models, ORM) |
---|
comment:8 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:11 by , 12 years ago
Cc: | added |
---|
comment:12 by , 12 years ago
1) Convention: the foreign key _without_ a related field is the field for the M2M relation every time.
2) A new option; something in the Meta class to identify the fields that should be used for the through.
[...]
Another option(which I'm not necessarily endorsing), would be to make through accept a tuple, the first arg is the intermediary model and the second is the name of the fkey to this model, and the third is the name of the fkey to the other model.
I find the third option to be the most natural notation. Also, I wonder what notation the first two options would have for an intermediary table that describes more than one M2M relationship...
comment:13 by , 10 years ago
This ticket seems to be already resolved in Django 1.7:
https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.through_fields
comment:14 by , 10 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Indeed, it seems that this ticket was a duplicate of #14549 which was fixed in c627da0ccc12861163f28177aa7538b420a9d310.
Thanks for the heads up!
If you read the ticket (#6095) and mailing list discussions surrounding this feature, this was discussed at the time.
There were a few suggestions on how to do this. Simply removing the check isn't an option - there is no reason to believe that the first foreign key will be the correct one.
Two possible options: