[Django] #34417: AlterField migration on ForeignKey field re-creates foreign key constraints unnecessarily

21 views
Skip to first unread message

Django

unread,
Mar 16, 2023, 12:23:01 PM3/16/23
to django-...@googlegroups.com
#34417: AlterField migration on ForeignKey field re-creates foreign key constraints
unnecessarily
------------------------------------------------+------------------------
Reporter: Ömer Faruk Abacı | Owner: nobody
Type: Cleanup/optimization | 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 |
------------------------------------------------+------------------------
Let's assume that we have the following models:

{{{
class Bar(models.Model):
pass

class Foo(models.Model):
bar = models.ForeignKey(Bar, related_name="foos",
on_delete=models.CASCADE)
}}}

The migration that creates these models will add an index to the `bar`
field of `Foo` model, this is well-documented and there is no problem up
until now. But when we do the following change to drop the index on the
ForeignKey field:


{{{
class Foo(models.Model):
bar = models.ForeignKey(..., db_index=False)
}}}

Then we have a migration that results in the following SQL statement:


{{{
BEGIN;
--
-- Alter field bar on foo
--
SET CONSTRAINTS "foos_foo_bar_id_efb062ad_fk_foos_bar_id" IMMEDIATE;
ALTER TABLE "foos_foo"
DROP CONSTRAINT "foos_foo_bar_id_efb062ad_fk_foos_bar_id";

DROP INDEX IF EXISTS "foos_foo_bar_id_efb062ad";

ALTER TABLE "foos_foo" ADD CONSTRAINT
"foos_foo_bar_id_efb062ad_fk_foos_bar_id"
FOREIGN KEY ("bar_id")
REFERENCES "bars_bar" ("id")
DEFERRABLE INITIALLY DEFERRED;
COMMIT;
}}}

I believe that we shouldn't be needing to re-create the FK constraint, so
the following SQL should suffice:


{{{
BEGIN;
--
-- Alter field bar on foo
--
DROP INDEX IF EXISTS "foos_foo_bar_id_efb062ad";
COMMIT;
}}}

Actually the main problem here is that dropping & adding constraints locks
the entire table, and it might be catastrophic for some live production
databases. IMHO this should either be documented or refactored. I look
forward to hear your opinions on this, thank you in advance!

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

Django

unread,
Mar 16, 2023, 3:31:06 PM3/16/23
to django-...@googlegroups.com
#34417: AlterField migration on ForeignKey field re-creates foreign key constraints
unnecessarily
--------------------------------------+------------------------------------

Reporter: Ömer Faruk Abacı | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Simon Charette):

* stage: Unreviewed => Accepted


Comment:

Dropping the foreign key and recreating it [https://dbfiddle.uk/hgRUL6q6
is effectively not necessary on PostgreSQL] but
[https://dbfiddle.uk/OJ6KqwrW it is on MySQL] as this backend requires an
index on the ''referencing'' side of the relation and will silently
default to using an existing one if its present (see #31335 for more
details).

Given the contention issues associated with dropping a foreign key on
PostgreSQL I think it's a worthy investment to try to make things better
on databases that are not affected by this quirk.

Would you like
[https://docs.djangoproject.com/en/4.1/internals/contributing/writing-code
/submitting-patches/ to work on a patch]? The
[https://github.com/django/django/blob/d2b688b966f5d30414899549412d370e1317ddb8/django/db/backends/base/schema.py#L852-L1198
alter_field method is where you want to start looking] and you'll likely
need a feature flag to denote that foreign keys must be dropped or not
based on the backend.

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

Django

unread,
Mar 17, 2023, 11:10:02 AM3/17/23
to django-...@googlegroups.com
#34417: AlterField migration on ForeignKey field re-creates foreign key constraints
unnecessarily
--------------------------------------+------------------------------------
Reporter: Ömer Faruk Abacı | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Ömer Faruk Abacı):

Hello Simon, thank you for your review! If that's OK for you one of my
colleagues encountered this issue will work on a patch for this on Monday.

--
Ticket URL: <https://code.djangoproject.com/ticket/34417#comment:2>

Django

unread,
Mar 17, 2023, 12:48:47 PM3/17/23
to django-...@googlegroups.com
#34417: AlterField migration on ForeignKey field re-creates foreign key constraints
unnecessarily
--------------------------------------+------------------------------------
Reporter: Ömer Faruk Abacı | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Simon Charette):

Totally okay Ömer, thanks to you and your coworker for taking the time to
submit a patch.

--
Ticket URL: <https://code.djangoproject.com/ticket/34417#comment:3>

Django

unread,
Mar 20, 2023, 11:08:16 AM3/20/23
to django-...@googlegroups.com
#34417: AlterField migration on ForeignKey field re-creates foreign key constraints
unnecessarily
--------------------------------------+------------------------------------
Reporter: Ömer Faruk Abacı | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Deniz Altun):

Hello Simon, just letting you know that I will be working on this patch.

--
Ticket URL: <https://code.djangoproject.com/ticket/34417#comment:4>

Django

unread,
Mar 20, 2023, 2:27:32 PM3/20/23
to django-...@googlegroups.com
#34417: AlterField migration on ForeignKey field re-creates foreign key constraints
unnecessarily
--------------------------------------+------------------------------------
Reporter: Ömer Faruk Abacı | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Simon Charette):

Sounds good, you'll want to assign the ticket to you then.

--
Ticket URL: <https://code.djangoproject.com/ticket/34417#comment:5>

Django

unread,
Mar 21, 2023, 6:39:33 AM3/21/23
to django-...@googlegroups.com
#34417: AlterField migration on ForeignKey field re-creates foreign key constraints
unnecessarily
-------------------------------------+-------------------------------------
Reporter: Ömer Faruk Abacı | Owner: Deniz
Type: | Altun
Cleanup/optimization | Status: assigned

Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Deniz Altun):

* owner: nobody => Deniz Altun
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/34417#comment:6>

Django

unread,
Jun 12, 2023, 5:48:10 AM6/12/23
to django-...@googlegroups.com
#34417: AlterField migration on ForeignKey field re-creates foreign key constraints
unnecessarily
-------------------------------------+-------------------------------------
Reporter: Ömer Faruk Abacı | Owner: Deniz
Type: | Altun
Cleanup/optimization | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1
* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/16922 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/34417#comment:7>

Django

unread,
Apr 30, 2024, 3:55:45 PMApr 30
to django-...@googlegroups.com
#34417: AlterField migration on ForeignKey field re-creates foreign key constraints
unnecessarily
--------------------------------------+------------------------------------
Reporter: Ömer Faruk Abacı | Owner: Bhuvnesh
Type: Cleanup/optimization | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Bhuvnesh):

* needs_better_patch: 1 => 0
* owner: Deniz Altun => Bhuvnesh

--
Ticket URL: <https://code.djangoproject.com/ticket/34417#comment:8>

Django

unread,
May 3, 2024, 7:18:53 AMMay 3
to django-...@googlegroups.com
#34417: AlterField migration on ForeignKey field re-creates foreign key constraints
unnecessarily
--------------------------------------+------------------------------------
Reporter: Ömer Faruk Abacı | Owner: Bhuvnesh
Type: Cleanup/optimization | Status: assigned
Component: Migrations | Version: 4.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/34417#comment:9>
Reply all
Reply to author
Forward
0 new messages