#35956: Add composite foreign keys
-------------------------------------+-------------------------------------
     Reporter:  Csirmaz Bendegúz     |                    Owner:  (none)
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Clifford Gama):

 Replying to [comment:4 Csirmaz Bendegúz]:
 > Replying to [comment:2 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:
 > > {{{#!python
 > > 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**.

 I'm +1 on sharing fields, but I think that Felix's suggestion should work
 as the default.

 In simple cases, having the foreign key automatically infer and create the
 needed fields is great developer experience — it keeps things intuitive
 and close to how `ForeignKey` works today.

 That said, we should definitely allow shared fields. For example, if a
 model already has a `tenant_id` FK to `Tenant`, and also needs to
 reference a model with a composite PK like `(tenant_id, id)`, it should be
 able to reuse the same `tenant_id` field in both relationships.

 So if possible, maybe we should have implicit field creation as default,
 and `to|from_fields` as an opt-in for more advanced use cases
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35956#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/010701965972d08a-7c4d79b2-b74f-4772-a9a3-c6897a712e69-000000%40eu-central-1.amazonses.com.

Reply via email to