Opened 9 years ago

Closed 8 years ago

#25276 closed Cleanup/optimization (worksforme)

AlterField should raise an error when referenced field doesn't exist

Reported by: Jacek Bzdak Owned by: nobody
Component: Migrations Version: dev
Severity: Normal Keywords:
Cc: desecho@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Anton Samarchyan)

If I have following model in app foo:

class Foo(models.Model): 
  bar = models.CharField(max_length=500)
  

And I write (by hand) following migration:

class Migration(migrations.Migration):

    dependencies = [
        ('foo', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='foo',
            name='baz', #Notice there is a typo in field name
            field=modeld.CharField(max_length=100)
        ),
    ]
 

Currently this migration is a does nothing, however I feel that more appropriate action would be to notify user that he is trying to alter nonexistent field.

Current implementation is a NOOP because (see: https://github.com/django/django/blob/master/django/db/migrations/operations/fields.py#L178) it is implemented in following way:

       state.models[app_label, self.model_name_lower].fields = [
            (n, field if n == self.name else f)
            for n, f in
            state.models[app_label, self.model_name_lower].fields
        ]
       state.reload_model(app_label, self.model_name_lower)

If someone says this is a sensible feature request I may modify migration code to raise if there is an error.

I have actually lost some hours of my life due to this behaviour when I was writing a migration that moved models between applications.

Change History (4)

comment:1 by Tim Graham, 9 years ago

Summary: It would be very nice if django AlterField raised an error when ordered to alter that doesn't existAlterField should raise an error when referenced field doesn't exist
Triage Stage: UnreviewedAccepted
Type: New featureCleanup/optimization
Version: 1.8master

comment:2 by Anton Samarchyan, 8 years ago

Cc: desecho@… added

I can't reproduce. I am getting django.core.exceptions.FieldDoesNotExist: Foo has no field named 'baz'.

comment:3 by Anton Samarchyan, 8 years ago

Description: modified (diff)

comment:4 by Tim Graham, 8 years ago

Resolution: worksforme
Status: newclosed

I also can't reproduce.

Note: See TracTickets for help on using tickets.
Back to Top