[Django] #35635: Adding a field with a default in migrations following an index rename seems to fail

11 views
Skip to first unread message

Django

unread,
Jul 26, 2024, 8:46:24 PM7/26/24
to django-...@googlegroups.com
#35635: Adding a field with a default in migrations following an index rename seems
to fail
-------------------------------------+-------------------------------------
Reporter: Raphael Gaschignard | Type: Bug
Status: new | Component:
| Migrations
Version: 5.0 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
I have the following migration:

{{{
operations = [
migrations.RenameIndex(
model_name="taggeditem",
new_name="taggit_tagg_content_8fc721_idx",
old_fields=("content_type", "object_id"),
),
]

}}}

I have created another migration after this one:
{{{
dependencies = [
(
"taggit",
"0006_rename_taggeditem_content_type_object_id_taggit_tagg_content_8fc721_idx",
),
]

operations = [
migrations.AddField(
model_name="taggeditem",
name="created_at",
field=models.DateTimeField(
blank=True, default=django.utils.timezone.now, null=True
),
),
]
}}}

This gives the following for `sqlmigrate`:
{{{
BEGIN;
--
-- Add field created_at to taggeditem
--
CREATE TABLE "new__taggit_taggeditem" ("id" integer NOT NULL PRIMARY KEY
AUTOINCREMENT, "created_at" datetime NULL, "object_id" integer NOT NULL,
"content_type_id" integer NOT NULL REFERENCES "django_content_type" ("id")
DEFERRABLE INITIALLY DEFERRED, "tag_id" integer NOT NULL REFERENCES
"taggit_tag" ("id") DEFERRABLE INITIALLY DEFERRED, CONSTRAINT
"taggit_taggeditem_content_type_id_object_id_tag_id_4bb97a8e_uniq" UNIQUE
("content_type_id", "object_id", "tag_id"));
INSERT INTO "new__taggit_taggeditem" ("id", "object_id",
"content_type_id", "tag_id", "created_at") SELECT "id", "object_id",
"content_type_id", "tag_id", '2024-07-27 00:31:47.134946' FROM
"taggit_taggeditem";
DROP TABLE "taggit_taggeditem";
ALTER TABLE "new__taggit_taggeditem" RENAME TO "taggit_taggeditem";
CREATE INDEX "taggit_taggeditem_object_id_e2d7d1df" ON "taggit_taggeditem"
("object_id");
CREATE INDEX "taggit_taggeditem_content_type_id_9957a03c" ON
"taggit_taggeditem" ("content_type_id");
CREATE INDEX "taggit_taggeditem_tag_id_f4f5b767" ON "taggit_taggeditem"
("tag_id");
CREATE INDEX "taggit_tagg_content_8fc721_idx" ON "taggit_taggeditem"
("content_type_id", "object_id");
CREATE INDEX "taggit_tagg_content_8fc721_idx" ON "taggit_taggeditem"
("content_type_id", "object_id");
COMMIT;
}}}

Note in particular the creating of two indexes with the same name.


Now, if I don't set a default in the `AddField` operation , the
`sqlmigrate` result is simply as follows:

{{{
BEGIN;
--
-- Add field created_at to taggeditem
--
ALTER TABLE "taggit_taggeditem" ADD COLUMN "created_at" datetime NULL;
COMMIT;
}}}

And if I comment out the `RenameIndex` operation, I only create the index
once, not twice, avoiding the issue.

Extra detail: I hit this because I have an index with a name, and then a
`RenameIndex` operation that renames it.
It seems like we would expect there to be a cleanup operation during a
rename. Instead, it creates the migration twice.

I have only tested this out with the SQLite driver, and I have a suspicion
it's only happening there, because... well... this seems like something
that would break elsewhere.

Reproduced this with 5.1rc1 and 5.0.6

Reproduction:

- Checkout https://github.com/jazzband/django-taggit/tree/ticket-700
- Install Django
- Run `sample_taggit/manage.py migrate`
--
Ticket URL: <https://code.djangoproject.com/ticket/35635>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 26, 2024, 8:51:31 PM7/26/24
to django-...@googlegroups.com
#35635: Adding a field with a default in migrations following an index rename seems
to fail
-------------------------------------+-------------------------------------
Reporter: Raphael Gaschignard | Owner: (none)
Type: Bug | Status: new
Component: Migrations | Version: 5.0
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
-------------------------------------+-------------------------------------
Description changed by Raphael Gaschignard:

Old description:
New description:
taggit/commit/27640cebf6d7f03f5b34d0395bbf85422a6ef51f
- Install Django
- Run `sample_taggit/manage.py migrate`

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35635#comment:1>

Django

unread,
Jul 28, 2024, 6:41:36 AM7/28/24
to django-...@googlegroups.com
#35635: Adding a field with a default in migrations following an index rename seems
to fail
-------------------------------------+-------------------------------------
Reporter: Raphael Gaschignard | Owner: (none)
Type: Bug | Status: closed
Component: Migrations | Version: 5.0
Severity: Normal | Resolution: needsinfo
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 David Sanders):

* resolution: => needsinfo
* status: new => closed

Comment:

Hi & thanks for the report.

Although I observed the exception on the branch you've linked, I couldn't
reproduce this with a simple example following the steps provided.

My advice would be to reproduce this either with the simplest setup
possible or through a unit test then paste the details here 👍

Closing with needsinfo for now until more info or someone with more time
can investigate and confirm the presence of a bug with Django,
--
Ticket URL: <https://code.djangoproject.com/ticket/35635#comment:2>
Reply all
Reply to author
Forward
0 new messages