#35129: Support customize ForeignKey DDL when db_constraint=False
-------------------------------------+-------------------------------------
     Reporter:  elonzh               |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  closed
    Component:  Database layer       |                  Version:  5.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  invalid
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

 I think this will eventually come in a form or the other eventually
 through a `Relation` object
 [http://charettes.name/djangoconus2022/slides.html#39 that can be defined
 at the model level] but in the mean time your best bet is to use (at your
 own risks) the undocumented `django.db.models.ForeignObject` object to
 define such relationships.

 {{{#!python
 class Journal(Model):
     issn = models.CharField(max_length=9, primary_key=True)
     name = models.CharField(max_length=255)

 class Biblio(Model):
     issn = models.CharField(max_length=50, default="", blank=True)
     journal = models.ForeignObject(
         Journal,
         models.DO_NOTHING,
         from_fields=["issn"],
         to_fields=["issn"],
     )
 }}}

 Under the hood `ForeignKey` is `Field` + `ForeignObject` + the equivalent
 of a `ForeignKeyConstraint(ForeignObject)` entry in
 `Model._meta.constraints` so if you care about the ORM + JOIN generation
 part and don't want a database constraint it's likely what you're looking
 for.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/35129#comment:4>
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 on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018d227db47f-ca9f6b6e-dba6-4882-9444-a751d299cbc9-000000%40eu-central-1.amazonses.com.

Reply via email to