Opened 6 years ago
Last modified 6 years ago
#30741 closed Bug
Setting db_constraint=False to an existing column does not properly drop the constraint — at Initial Version
| 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
Repro steps, using PostgreSQL and Django 2.2.4:
- 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)
- Create a migration: python manage.py makemigrations
- 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)
- Create a second migration: python manage.py makemigrations
- 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;
Note:
See TracTickets
for help on using tickets.