Opened 5 years ago

Last modified 5 years ago

#30741 closed Bug

Setting db_constraint=False to an existing column does not properly drop the constraint — at Version 1

Reported by: Scott Stafford Owned by: nobody
Component: Migrations Version: dev
Severity: Normal Keywords: db_constraint, migrations
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Scott Stafford)

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)
  1. Create a migration: python manage.py makemigrations
  1. 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)
  1. Create a second migration: python manage.py makemigrations
  1. 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.

Change History (1)

comment:1 by Scott Stafford, 5 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top