Opened 9 years ago
Closed 9 years ago
#26794 closed Bug (duplicate)
Migrating ForeignKey to OneToOneField creates extra index on PostgreSQL
Reported by: | Jon Dufresne | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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'), ), ]
Duplicate of #26090