Opened 5 weeks ago
Last modified 9 days ago
#35956 new New feature
Add composite foreign keys
Reported by: | Csirmaz Bendegúz | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Csirmaz Bendegúz, Mariusz Felisiak | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This is a follow up to #373 (CompositePrimaryKey).
Now that composite primary keys are merged, it would be great to be able to create foreign keys referencing them through the Django ORM.
My proposal is to add 2 parameters to ForeignKey
: from_fields
and to_fields
.
They would map to the underlying ForeignObject
's from_fields
, to_fields
parameters.
If a ForeignKey
has multiple fields, it acts as a virtual field, meaning it doesn't create a database column automatically.
Change History (4)
comment:1 by , 5 weeks ago
Triage Stage: | Unreviewed → Accepted |
---|
follow-up: 4 comment:2 by , 2 weeks ago
comment:3 by , 2 weeks ago
Cc: | added |
---|
comment:4 by , 9 days ago
Replying to Mariusz Felisiak:
Do we need these parameters?
CompositePrimaryKey
is always a primary key so its fields should be detected automatically, IMO, the following example should work:
class Release(models.Model): pk = models.CompositePrimaryKey("version", "name") version = models.IntegerField() name = models.CharField(max_length=20) class RefRelease(models.Model): release = models.ForeignKey("Release", models.CASCADE)
The issue with this example is RefRelease
must have 2 fields matching the CompositePrimaryKey
(an IntegerField()
and a CharField(max_length=20)
).
So release
would need to create 2 fields implicitly and I think that's too restrictive.
We should allow sharing fields between composite foreign keys.
For example, in a multi-tenant app where each model has a CompositePrimaryKey("tenant_id", "id")
, the tenant_id
field should be shared between composite foreign keys.
Let me know what you think about this, I'm happy to consider alternatives.
Replying to Csirmaz Bendegúz:
Do we need these parameters?
CompositePrimaryKey
is always a primary key so its fields should be detected automatically, IMO, the following example should work: