#26794: Migrating ForeignKey to OneToOneField creates extra index on PostgreSQL
----------------------------+--------------------
Reporter: jdufresne | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
Start with the models:
{{{
class A(models.Model):
pass
class B(models.Model):
orig_onetoone = models.OneToOneField(A, on_delete=models.PROTECT,
related_name='+')
orig_fk = models.ForeignKey(A, on_delete=models.PROTECT,
related_name='+')
}}}
Run `makemigrations`, then change `orig_fk` to a `OneToOneField`:
{{{
class A(models.Model):
pass
class B(models.Model):
orig_onetoone = models.OneToOneField(A, on_delete=models.PROTECT,
related_name='+')
orig_fk = models.OneToOneField(A, on_delete=models.PROTECT,
related_name='+')
}}}
Run `makemigrations` again.
Now run `migrate` to create the database tables. The table created for
model `B`:
{{{
\d testapp_b
Table "public.testapp_b"
Column | Type | Modifiers
------------------+---------+--------------------------------------------------------
id | integer | not null default
nextval('testapp_b_id_seq'::regclass)
orig_fk_id | integer | not null
orig_onetoone_id | integer | not null
Indexes:
"testapp_b_pkey" PRIMARY KEY, btree (id)
"testapp_b_orig_fk_id_68b3dcb4_uniq" UNIQUE CONSTRAINT, btree
(orig_fk_id)
"testapp_b_orig_onetoone_id_key" UNIQUE CONSTRAINT, btree
(orig_onetoone_id)
"testapp_b_ca8b5791" btree (orig_fk_id)
Foreign-key constraints:
"testapp_b_orig_fk_id_68b3dcb4_fk_testapp_a_id" FOREIGN KEY
(orig_fk_id) REFERENCES testapp_a(id) DEFERRABLE INITIALLY DEFERRED
"testapp_b_orig_onetoone_id_2353312c_fk_testapp_a_id" FOREIGN KEY
(orig_onetoone_id) REFERENCES testapp_a(id) DEFERRABLE INITIALLY DEFERRED
}}}
Notice the column `orig_fk` has two indexes while the column
`orig_onetoone` has only a single index.
As these two columns are now described in the Python model identically, I
expect them to have the same number of indexes.
I created a test project with all the necessary migrations and models used
to produce these results: https://github.com/jdufresne/djtest-fk-to-1to1
Migrations:
{{{
0001
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='A',
fields=[
('id', models.AutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='B',
fields=[
('id', models.AutoField(auto_created=True,
primary_key=True, serialize=False, verbose_name='ID')),
('orig_fk',
models.ForeignKey(on_delete=django.db.models.deletion.PROTECT,
related_name='+', to='testapp.A')),
('orig_onetoone',
models.OneToOneField(on_delete=django.db.models.deletion.PROTECT,
related_name='+', to='testapp.A')),
],
),
]
0002
class Migration(migrations.Migration):
dependencies = [
('testapp', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='b',
name='orig_fk',
field=models.OneToOneField(on_delete=django.db.models.deletion.PROTECT,
related_name='+', to='testapp.A'),
),
]
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26794>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.add9ba80a3002ae996aab1463617771e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.