[Django] #25317: Altering a field to ForeignKey does not create index

16 views
Skip to first unread message

Django

unread,
Aug 26, 2015, 11:09:47 AM8/26/15
to django-...@googlegroups.com
#25317: Altering a field to ForeignKey does not create index
-------------------------------+--------------------
Reporter: synotna | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
Changing a OneToOneField to a ForeignKey does not trigger an index to be
created by the migration


{{{
migrations.AlterField(
model_name='contracttwitchchannelrevenueadsbycountry',
name='revenue_data',
field=models.ForeignKey(to='twitchrevenue.TwitchRevenueDataAdsByCountry'),
preserve_default=True,
),
}}}

SQL for the migration:


{{{
./manage.py sqlmigrate partners 0021

BEGIN;
ALTER TABLE "partners_contracttwitchchannelrevenueadsbycountry" DROP
CONSTRAINT "a45496b32304e658c2a94795584a1889";
ALTER TABLE "partners_contracttwitchchannelrevenueadsbycountry" ADD
CONSTRAINT "D321aaa5509069df3ee6b51c0eb97103" FOREIGN KEY
("revenue_data_id") REFERENCES
"twitchrevenue_twitchrevenuedataadsbycountry" ("id") DEFERRABLE INITIALLY
DEFERRED;

COMMIT;
}}}


Also, explicitly stating db_index=True does not cause a new migration to
be created

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

Django

unread,
Aug 26, 2015, 2:49:12 PM8/26/15
to django-...@googlegroups.com
#25317: Altering a field to ForeignKey does not create index
-------------------------------+--------------------------------------
Reporter: synotna | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 1.8
Severity: Normal | Resolution: invalid
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 shaib):

* status: new => closed
* needs_better_patch: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_docs: => 0


Comment:

Why should one be created? If it was a !OneToOneField, it should already
have an index, and I see nothing there to drop it.

I suppose you came to the conclusion that something was wrong because
something failed for you; it would be more useful to describe the problem
you encountered and the process that led you to the conclusion that the
behavior was wrong (but, the proper place to do so would be a users forum
like the [https://groups.google.com/d/forum/django-users django-users
group]).

I am closing the ticket as invalid based on the info given; feel free to
reopen (and explain) if I've misunderstood, or to open a separate ticket
if appropriate.

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

Django

unread,
Aug 31, 2015, 4:07:09 AM8/31/15
to django-...@googlegroups.com
#25317: Altering a field to ForeignKey does not create index
-------------------------------+--------------------------------------
Reporter: synotna | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 1.8
Severity: Normal | Resolution: invalid
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 synotna):

Sorry, you are correct: I did not make the connection that the
OneToOneField should have already created the index beforehand

Here is the migration that created the model including the OneToOneField,
and the output from sqlmigrate which shows that index creation was
missing:


{{{
migrations.CreateModel(
name='ContractTwitchChannelRevenueAdsByCountry',
fields=[
('id', models.AutoField(primary_key=True, serialize=False,
auto_created=True, verbose_name='ID')),
('partner_revenue', models.DecimalField(decimal_places=7,
max_digits=12)),
('contract_twitch_channel',
models.ForeignKey(to='partners.ContractTwitchChannel')),
('revenue_data',
models.OneToOneField(to='twitchrevenue.TwitchRevenueDataAdsByCountry')),
],
options={
},
bases=(models.Model,),
),
}}}


{{{
./manage.py sqlmigrate partners 0007

BEGIN;
CREATE TABLE "partners_contracttwitchchannelrevenueadsbycountry" ("id"
serial NOT NULL PRIMARY KEY, "partner_revenue" numeric(12, 7) NOT NULL,
"contract_twitch_channel_id" integer NOT NULL, "revenue_data_id" integer
NOT NULL UNIQUE);
ALTER TABLE "partners_contracttwitchchannelrevenueadsbycountry" ADD
CONSTRAINT "ccbec8ea2680b0e44e335452dd041304" FOREIGN KEY
("contract_twitch_channel_id") REFERENCES "partners_contracttwitchchannel"


("id") DEFERRABLE INITIALLY DEFERRED;

ALTER TABLE "partners_contracttwitchchannelrevenueadsbycountry" ADD
CONSTRAINT "a1dfe1f83ea9a2dbb28dc2ec53ef0136" FOREIGN KEY


("revenue_data_id") REFERENCES
"twitchrevenue_twitchrevenuedataadsbycountry" ("id") DEFERRABLE INITIALLY
DEFERRED;

CREATE INDEX "partners_contracttwitchchannelrevenueadsbycountry_71b8390c"
ON "partners_contracttwitchchannelrevenueadsbycountry"
("contract_twitch_channel_id");

COMMIT;
}}}

Am I correct in thinking that is a bug? If so then I'll edit (or open) a
new bug ticket correctly

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

Django

unread,
Aug 31, 2015, 4:13:59 AM8/31/15
to django-...@googlegroups.com
#25317: Altering a field to ForeignKey does not create index
-------------------------------+--------------------------------------
Reporter: synotna | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 1.8
Severity: Normal | Resolution: invalid
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 synotna):

* cc: a@… (added)


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

Django

unread,
Aug 31, 2015, 11:21:09 AM8/31/15
to django-...@googlegroups.com
#25317: Altering a field to ForeignKey does not create index
-------------------------------+--------------------------------------
Reporter: synotna | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 1.8
Severity: Normal | Resolution: invalid
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 timgraham):

My understanding is that an index
[https://docs.djangoproject.com/en/1.8/ref/models/fields/#unique shouldn't
be created] in this case since `OneToOneField` is `unique=True`: "Note
that when `unique` is `True`, you don’t need to specify `db_index`,
because `unique` implies the creation of an index."

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

Django

unread,
Sep 3, 2015, 7:52:21 PM9/3/15
to django-...@googlegroups.com
#25317: Altering a 1to1 field to FK on Postgres drops uniqueness but does not
create index
-------------------------------+------------------------------------

Reporter: synotna | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: 1.8
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 shaib):

* cc: shaib (added)
* status: closed => new
* resolution: invalid =>
* stage: Unreviewed => Accepted


Comment:

Ok, I see now. The PG backend relies on the unique constraint to create an
index (correctly) for a 1-1 field; which means, if the field is no longer
unique but still a FK, it needs an explicit index.

(you can probably work around this by adding a single-field
`index_together` entry to your Meta)

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

Django

unread,
Sep 4, 2015, 10:14:32 AM9/4/15
to django-...@googlegroups.com
#25317: Altering a 1to1 field to FK on Postgres drops uniqueness but does not
create index
----------------------------+------------------------------------

Reporter: synotna | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8

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 claudep):

* component: Uncategorized => Migrations


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

Django

unread,
Jan 19, 2016, 8:35:28 AM1/19/16
to django-...@googlegroups.com
#25317: Altering a 1to1 field to FK on Postgres drops uniqueness but does not
create index
----------------------------+------------------------------------
Reporter: synotna | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
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 timgraham):

See also the opposite case of `ForeignKey` to `OneToOneField` in #26090.

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

Django

unread,
Jun 30, 2016, 1:11:28 AM6/30/16
to django-...@googlegroups.com
#25317: Altering a 1to1 field to FK on Postgres drops uniqueness but does not
create index
----------------------------+------------------------------------
Reporter: synotna | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8
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 jdufresne):

* has_patch: 0 => 1


Comment:

https://github.com/django/django/pull/6851

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

Django

unread,
Jul 7, 2016, 8:33:00 PM7/7/16
to django-...@googlegroups.com
#25317: Altering a 1to1 field to FK on Postgres drops uniqueness but does not
create index
----------------------------+------------------------------------
Reporter: synotna | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.8
Severity: Normal | Resolution: fixed

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 Tim Graham <timograham@…>):

* status: new => closed

* resolution: => fixed


Comment:

In [changeset:"9356f63a99957f01c14a9788617428a172a29fcb" 9356f63a]:
{{{
#!CommitTicketReference repository=""
revision="9356f63a99957f01c14a9788617428a172a29fcb"
Fixed #25317, #26090 -- Fixed swapping combinations of unique and db_index
during migrations.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25317#comment:9>

Reply all
Reply to author
Forward
0 new messages