Opened 4 years ago
Closed 4 years ago
#31831 closed Bug (fixed)
AlterOrderWithRespectTo() with ForeignKey crash when _order is included in Index().
Reported by: | KyuJoo Han | Owned by: | Iuri de Silvio |
---|---|---|---|
Component: | Migrations | Version: | 3.0 |
Severity: | Normal | Keywords: | migrations |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
class Meta: db_table = 'look_image' order_with_respect_to = 'look' indexes = [ models.Index(fields=['look', '_order']), models.Index(fields=['created_at']), models.Index(fields=['updated_at']), ]
migrations.CreateModel( name='LookImage', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('look', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='images', to='posts.Look', verbose_name='LOOK')), ('image_url', models.URLField(blank=True, max_length=10000, null=True)), ('image', models.ImageField(max_length=2000, upload_to='')), ('deleted', models.DateTimeField(editable=False, null=True)), ('created_at', models.DateTimeField(auto_now_add=True)), ('updated_at', models.DateTimeField(auto_now=True)), ], ), migrations.AddIndex( model_name='lookimage', index=models.Index(fields=['look', '_order'], name='look_image_look_id_eaff30_idx'), ), migrations.AddIndex( model_name='lookimage', index=models.Index(fields=['created_at'], name='look_image_created_f746cf_idx'), ), migrations.AddIndex( model_name='lookimage', index=models.Index(fields=['updated_at'], name='look_image_updated_aceaf9_idx'), ), migrations.AlterOrderWithRespectTo( name='lookimage', order_with_respect_to='look', ),
I added orders_with_respect_to in new model class's Meta class and also made index for '_order' field by combining with other field. And a new migration file based on the model looks like the code above.
The problem is operation AlterOrderWithRespectTo after AddIndex of '_order' raising error because '_order' field had not been created yet.
It seems to be AlterOrderWithRespectTo has to proceed before AddIndex of '_order'.
Change History (8)
comment:1 by , 4 years ago
Component: | Core (Management commands) → Migrations |
---|---|
Summary: | AlterOrderWithRespectTo has to proceed before AddIndex of '_order' field → AlterOrderWithRespectTo() with ForeignKey crash when _order is included in Index(). |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 4 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
I reproduced the issue adding order_with_respect_to
and indexes = [models.Index(fields='_order')]
at the same time to an existent model.
class Meta: order_with_respect_to = 'foo' indexes = [models.Index(fields='_order')]
A small broken test: https://github.com/iurisilvio/django/commit/5c6504e67f1d2749efd13daca440dfa54708a4b2
I'll try to fix the issue, but I'm not sure the way to fix it. Can we reorder autodetected migrations?
comment:4 by , 4 years ago
Patch needs improvement: | set |
---|
comment:5 by , 4 years ago
Patch needs improvement: | unset |
---|
comment:6 by , 4 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Thanks for this report. IMO
order_with_respect_to
should be included inCreateModel()
'soptions
, I'm not sure why it is in a separate operation when it refers to aForeignKey
.