{{{
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.
* 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>
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>
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>
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>
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>
* owner: nobody => Deniz Altun
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/34417#comment:6>
* 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>