#32634: AlterField drops contraints in the wrong order when performed as reverse
migration
----------------------------------+--------------------------------------
     Reporter:  Matthias Dellweg  |                    Owner:  nobody
         Type:  Bug               |                   Status:  new
    Component:  Migrations        |                  Version:  3.2
     Severity:  Normal            |               Resolution:
     Keywords:                    |             Triage Stage:  Unreviewed
    Has patch:  0                 |      Needs documentation:  0
  Needs tests:  0                 |  Patch needs improvement:  0
Easy pickings:  0                 |                    UI/UX:  0
----------------------------------+--------------------------------------
Changes (by Matthias Dellweg):

 * status:  closed => new
 * version:  2.2 => 3.2
 * resolution:  needsinfo =>


Comment:

 I could reproduce with Django==3.2.

 I created a sample project here:
 https://github.com/mdellweg/migrationtest

 If you configure this app to a postgres driver and call
 `migrationtest/manage.py sqlmigrate --backwards pk_migration 0002`, you
 will see:
 {{{
 BEGIN;
 --
 -- Alter field secondbase_ptr on child
 --
 SET CONSTRAINTS
 "pk_migration_child_secondbase_ptr_id_9b785ce8_fk_pk_migrat" IMMEDIATE;
 ALTER TABLE "pk_migration_child" DROP CONSTRAINT
 "pk_migration_child_secondbase_ptr_id_9b785ce8_fk_pk_migrat";
 ALTER TABLE "pk_migration_child" ALTER COLUMN "secondbase_ptr_id" DROP NOT
 NULL;
 ALTER TABLE "pk_migration_child" DROP CONSTRAINT
 "pk_migration_child_secondbase_ptr_id_9b785ce8_pk";
 ALTER TABLE "pk_migration_child" ADD CONSTRAINT
 "pk_migration_child_secondbase_ptr_id_9b785ce8_uniq" UNIQUE
 ("secondbase_ptr_id");
 ALTER TABLE "pk_migration_child" ADD CONSTRAINT
 "pk_migration_child_secondbase_ptr_id_9b785ce8_fk_pk_migrat" FOREIGN KEY
 ("secondbase_ptr_id") REFERENCES "pk_migration_secondbase" ("id")
 DEFERRABLE INITIALLY DEFERRED;
 --
 -- Remove field firstbase_ptr from child
 --
 ALTER TABLE "pk_migration_child" ADD COLUMN "firstbase_ptr_id" bigint NOT
 NULL PRIMARY KEY CONSTRAINT
 "pk_migration_child_firstbase_ptr_id_3ea33ffb_fk_pk_migrat" REFERENCES
 "pk_migration_firstbase"("id") DEFERRABLE INITIALLY DEFERRED; SET
 CONSTRAINTS "pk_migration_child_firstbase_ptr_id_3ea33ffb_fk_pk_migrat"
 IMMEDIATE;
 --
 -- MIGRATION NOW PERFORMS OPERATION THAT CANNOT BE WRITTEN AS SQL:
 -- Raw Python operation
 --
 --
 -- Add field secondbase_ptr to child
 --
 SET CONSTRAINTS
 "pk_migration_child_secondbase_ptr_id_9b785ce8_fk_pk_migrat" IMMEDIATE;
 ALTER TABLE "pk_migration_child" DROP CONSTRAINT
 "pk_migration_child_secondbase_ptr_id_9b785ce8_fk_pk_migrat";
 ALTER TABLE "pk_migration_child" DROP COLUMN "secondbase_ptr_id" CASCADE;
 COMMIT;
 }}}
 Which will fail to migrate with `column "secondbase_ptr_id" is in a
 primary key` when trying to execute `ALTER TABLE "pk_migration_child"
 ALTER COLUMN "secondbase_ptr_id" DROP NOT NULL;`.

 If you move `ALTER TABLE "pk_migration_child" DROP CONSTRAINT
 "pk_migration_child_secondbase_ptr_id_9b785ce8_pk";` one place up in the
 command chain, the migration will succeed.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32634#comment:2>
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/066.991329e7b4a38235b2b19ba2263e8dea%40djangoproject.com.

Reply via email to