﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35129	Support customize ForeignKey DDL when db_constraint=False	elonzh	nobody	"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."	Uncategorized	closed	Database layer (models, ORM)	5.0	Normal	invalid			Unreviewed	0	0	0	0	0	0
