0
votes

Although a couple of similar question were posted, none of those questions are the same as my case.

I had a model that looked like this:

class Person(models.Model):
"""Definition of persons that will fulfill a role in a committee
or will be in a way associated with a committee as an administrator
"""
ClientId = models.ForeignKey('clients.Client', on_delete=models.CASCADE,
                             to_field='id')
PersNumber = models.PositiveIntegerField(null=False)
PersSurName = models.CharField(max_length=40, null=False)
PersNames = models.CharField(max_length=40, null=False)

I set the uniqueness of the record on ClientId and PersNumber.

I have created 3 records in the database. Along the way I became convinced that I might as well use the auto generated id of the record as the person number (I am learning Django).

I removed the PersNumber from my model and ran makemigrations.

All is well until I ran migrate...

I get the following error:

django.core.exceptions.FieldDoesNotExist: Person has no field named 'PersNumber'

Any idee on how to get past this error

The full trace looks like this:

Operations to perform:

Apply all migrations: admin, auth, clients, contenttypes, komadm_conf, sessions Running migrations: Applying komadm_conf.0017_auto_20180830_1806...Traceback (most recent call last): File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\models\options.py", line 564, in get_field return self.fields_map[field_name] KeyError: 'PersNumber'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "manage.py", line 15, in execute_from_command_line(sys.argv) File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\core\management__init__.py", line 381, in execute_from_command_line utility.execute() File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\core\management__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv self.execute(*args, **cmd_options) File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\core\management\base.py", line 353, in execute output = self.handle(*args, **options) File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\core\management\base.py", line 83, in wrapped res = handle_func(*args, **kwargs) File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle fake_initial=fake_initial, File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial) File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration state = migration.apply(state, schema_editor) File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\migrations\migration.py", line 124, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\migrations\operations\fields.py", line 150, in database_forwards schema_editor.remove_field(from_model, from_model._meta.get_field(self.name)) File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\backends\sqlite3\schema.py", line 318, in remove_field self._remake_table(model, delete_field=field) File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\backends\sqlite3\schema.py", line 257, in _remake_table self.create_model(temp_model) File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\backends\base\schema.py", line 300, in create_model columns = [model._meta.get_field(field).column for field in fields] File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\backends\base\schema.py", line 300, in columns = [model._meta.get_field(field).column for field in fields] File "C:\ApplicationDef\za\co\drie_p\Komadmin.db\KomAdmin\KomadmTest\komadm_app\komadm_env\lib\site-packages\django\db\models\options.py", line 566, in get_field raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name)) django.core.exceptions.FieldDoesNotExist: Person has no field named 'PersNumber'

1

1 Answers

0
votes

Giving the fact that nobody else answered i will(but i am learning too)..

did you check if this field still exist ? (in the shell for instance).

i'll recommand to manually delete the migration file associated and try again (makemigrations + migrate).
The migration file i am talking about can be found MyProject/Myapp/migrations/00xx_something.py

If it still does not work you can delete the table directly in your database, and the migration file associated.