Opened 4 years ago

Closed 4 years ago

#21890 closed Bug (fixed)

django.core.exceptions.FieldError: Local field ... in class ... clashes with field of similar name from base class ...

Reported by: Claus Conrad Owned by:
Component: Migrations Version: 1.7-alpha-1
Severity: Normal Keywords:
Cc: lists@…, info@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Moving a field from a subclass to its base class results in the above error due to the operations being in the wrong order. Reordering the operations allowed me to run the migration.

Change History (5)

comment:1 Changed 4 years ago by Claus Conrad

Cc: lists@… added

comment:2 Changed 4 years ago by Baptiste Mispelon

Triage Stage: UnreviewedAccepted


I can reproduce the error you describer with the following models:

# First version
class Foo(models.Model):

class Bar(Foo):
    baz = models.IntegerField(default=42)

# Second version
class Foo(models.Model):
    baz = models.IntegerField(default=42)

class Bar(Foo):

After creating the version, I run makemigrations, then change my models to the second version and run makemigrations again.
After that, running migrate triggers the following error:

Running migrations:
  Applying bug21890.0001_initial... OK
  Applying bug21890.0002_auto_20140129_0404...Traceback (most recent call last):
  File "", line 10, in <module>
  File "./django/core/management/", line 427, in execute_from_command_line
  File "./django/core/management/", line 419, in execute
  File "./django/core/management/", line 287, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "./django/core/management/", line 336, in execute
    output = self.handle(*args, **options)
  File "./django/core/management/commands/", line 145, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "./django/db/migrations/", line 60, in migrate
    self.apply_migration(migration, fake=fake)
  File "./django/db/migrations/", line 94, in apply_migration
    migration.apply(project_state, schema_editor)
  File "./django/db/migrations/", line 97, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "./django/db/migrations/operations/", line 28, in database_forwards
    to_model = to_state.render().get_model(app_label, self.model_name)
  File "./django/db/migrations/", line 48, in render
  File "./django/db/migrations/", line 213, in render
  File "./django/db/models/", line 244, in __new__
    'base class %r' % (, name, base.__name__)
django.core.exceptions.FieldError: Local field 'baz' in class 'Bar' clashes with field of similar name from base class 'Foo'


comment:3 Changed 4 years ago by Markus Holtermann

Cc: info@… added

comment:4 Changed 4 years ago by ANUBHAV JOSHI

From what I have understood, the changes occur first in class Foo, thus it now has a field baz. Then when applying changes in class Bar, when it sees that parent class is Foo and Bar has a field of same name as that in Foo, it raises FieldError.

If such FieldError arises, then it should be caught and then we can create a whole new Bar or something like that.
Reordering will create subclass first, will it not be wrong creating a subclassbefore its base class


Last edited 4 years ago by ANUBHAV JOSHI (previous) (diff)

comment:5 Changed 4 years ago by Andrew Godwin

Resolution: fixed
Status: newclosed

This has since been fixed by the autodetector rewrite. You will lose data with this move, as it's a column being moved across tables; to avoid this, you would have to manually create a migration that made the new column, copied data across, and then removed the old column, and then gave the new column the correct name (it can't have it to start with as it would clash, like the original error here)

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