#30741: Setting db_constraint=False to an existing column does not properly drop
the constraint
-------------------------------------+-------------------------------------
     Reporter:  Scott Stafford       |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  Migrations           |                  Version:  2.2
     Severity:  Normal               |               Resolution:
     Keywords:  db_constraint,       |             Triage Stage:
  migrations                         |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Scott Stafford:

Old description:

> 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;
> }}}

New description:

 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.

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30741#comment:1>
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/063.1fcaa2562a3370699c77f893bbc66e23%40djangoproject.com.

Reply via email to