[Django] #33932: Error in migration with version 4.1

4 views
Skip to first unread message

Django

unread,
Aug 17, 2022, 5:52:28 AM8/17/22
to django-...@googlegroups.com
#33932: Error in migration with version 4.1
----------------------------------------+------------------------
Reporter: Benoît Vinot | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 4.1
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 |
----------------------------------------+------------------------
Hi, here is my problem. I have an independent table with a `id` primary
key. I would like this table to inherit from a new parent table. I have
generated migrations which work with Django version 4.0.7 but fail with
Django 4.1.

I have renamed the field `id` into `base_ptr` (`Base` is the new parent
table) and then, in another migration file, I try to make this column to
be a 1 to 1 column. To do that, I have in my migration an operation like
this

{{{

migrations.AlterField(
model_name="son",
name="base_ptr",
field=models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="app.Base",
),
),
}}}

which leads to this SQL code
{{{

BEGIN;
--
-- Alter field base_ptr on son
--
ALTER TABLE "app_son" RENAME COLUMN "base_ptr" TO "base_ptr_id";
ALTER TABLE "app_son" ALTER COLUMN "base_ptr" DROP IDENTITY IF EXISTS;
ALTER TABLE "app_son" ALTER COLUMN "base_ptr_id" TYPE integer;
DROP SEQUENCE IF EXISTS "app_son_base_ptr_id_seq" CASCADE;
ALTER TABLE "app_son" ADD CONSTRAINT
"app_son_base_ptr_id_d3673d48_fk_app_base" FOREIGN KEY ("base_ptr_id")
REFERENCES "app_base" ("id") DEFERRABLE INITIALLY DEFERRED;
--

}}}

This SQL code fails because the second line (`DROP IDENTITY EXISTS`)
references the `base_ptr` column which has been renamed into `base_ptr_id`
by the first line...

--
Ticket URL: <https://code.djangoproject.com/ticket/33932>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 17, 2022, 5:58:25 AM8/17/22
to django-...@googlegroups.com
#33932: Error in migration with version 4.1
-------------------------------------+-------------------------------------
Reporter: Benoît Vinot Roseau | Owner: nobody
Technologies |

Type: Bug | Status: new
Component: Migrations | Version: 4.1
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
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

Can you provide a sample project to reproduce? This transition is quite
tricky.

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

Reply all
Reply to author
Forward
0 new messages