#35129: Support customize ForeignKey DDL when db_constraint=False
-------------------------------------+-------------------------------------
               Reporter:  elonzh     |          Owner:  nobody
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  5.0
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 Sometimes we want to join tables without foreignkeys, so we define a
 ForeignKey field with db_constraint=False to achieve this.

 For example:

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

 class Biblio(Model):
     # There already exists a field called issn in the Biblio model
     # issn = models.CharField(max_length=50, default="", blank=True)
     journal = models.ForeignKey(
         Journal,
         related_name="+",
         db_column="issn",
         db_constraint=False,
         db_index=False,
         on_delete=models.DO_NOTHING,
         default="",
         blank=True,
         max_length=50,
     )
 }}}

 In this example, we define a journal field from issn field and we want to
 do a fake migration.

 But the migratation will generate DDL like this:


 {{{
 --
 -- Remove field issn from biblio
 --
 ALTER TABLE `userlibrary_biblio` DROP COLUMN `issn`;
 --
 -- Add field issn_record to biblio
 --
 ALTER TABLE `userlibrary_biblio` ADD COLUMN `issn` varchar(9) DEFAULT ''
 NOT NULL;
 ALTER TABLE `userlibrary_biblio` ALTER COLUMN `issn` DROP DEFAULT;
 }}}

 which is kind of awkward because that means Django will force max_length=9
 and this is not we want.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/35129>
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/0107018d211d1508-e20f7985-d0e0-45b1-8cc2-d52284ba8305-000000%40eu-central-1.amazonses.com.

Reply via email to