{{{
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.
* 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>
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>
* cc: a@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/25317#comment:3>
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>
* 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>
* component: Uncategorized => Migrations
--
Ticket URL: <https://code.djangoproject.com/ticket/25317#comment:6>
Comment (by timgraham):
See also the opposite case of `ForeignKey` to `OneToOneField` in #26090.
--
Ticket URL: <https://code.djangoproject.com/ticket/25317#comment:7>
* has_patch: 0 => 1
Comment:
https://github.com/django/django/pull/6851
--
Ticket URL: <https://code.djangoproject.com/ticket/25317#comment:8>
* 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>