Opened 6 weeks ago
Last modified 11 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 , 11 days ago
Replying to Mariusz Felisiak:
Replying to Csirmaz Bendegúz:
My proposal is to add 2 parameters to
ForeignKey
:from_fields
andto_fields
.
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'm not sure if this would be a good direction?
We should allow sharing fields between composite foreign keys, for example in a multitenant app, the tenant_id
should be shared between multiple composite foreign keys.
Let me know what you think, I'm happy to consider any 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: