﻿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
30741	Setting db_constraint=False to an existing column does not properly drop the constraint	Scott Stafford	nobody	"Repro steps, using PostgreSQL and Django 2.2.4:

1. Create a new model with a normal, constrained ForeignKey, such as:

{{{
class Child(models.Model):
    parent = models.ForeignKey('myapp.Parent', related_name=""+"", on_delete=models.DO_NOTHING)
}}}

2. Create a migration: python manage.py makemigrations

3. Add db_constraint=False, like so:

{{{
class Child(models.Model):
    parent = models.ForeignKey('myapp.Parent', related_name=""+"", on_delete=models.DO_NOTHING, db_constraint=False)
}}}

4. Create a second migration: python manage.py makemigrations

5. Peep at the generated SQL.  The first migration creates the constraint, and the second fails to remove it:

{{{
(.env) c:\wc\dbconstraintnotdropped>python manage.py sqlmigrate myapp 0001
BEGIN;
--
-- Create model Parent
--
CREATE TABLE ""myapp_parent"" (""id"" serial NOT NULL PRIMARY KEY);
--
-- Create model Child
--
CREATE TABLE ""myapp_child"" (""id"" serial NOT NULL PRIMARY KEY, ""parent_id"" integer NOT NULL);
ALTER TABLE ""myapp_child"" ADD CONSTRAINT ""myapp_child_parent_id_af46d0ab_fk_myapp_parent_id"" FOREIGN KEY (""parent_id"") REFERENCES ""myapp_parent"" (""id"") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX ""myapp_child_parent_id_af46d0ab"" ON ""myapp_child"" (""parent_id"");
COMMIT;

(.env) c:\wc\dbconstraintnotdropped>python manage.py sqlmigrate myapp 0002
BEGIN;
--
-- Alter field submission on child
--
COMMIT;
}}}

Presumably related to the changes made on ticket:23264.

"	Uncategorized	new	Migrations	2.2	Normal		db_constraint, migrations		Unreviewed	0	0	0	0	0	0
