Home > Net >  python3 manage.py migrate gives error about field even when it is deleted from the model class
python3 manage.py migrate gives error about field even when it is deleted from the model class

Time:03-08

Every time I run python3 manage.py migrate I get the same error about one of the fields in the model class. Even after deleting the field, the same error occurs.

This is what the model class looks like:

class Events(models.Model):
    name = models.CharField(max_length=200, null=True)
    date = models.DateTimeField(editable=True, null=True)
    sport = models.CharField(max_length=200, null=True)
    location = models.CharField(max_length=200, null=True)
    description = models.CharField(max_length=200, null=True, blank=True)
    date_created = models.DateTimeField(auto_now_add=True)
    tags = models.ManyToManyField(Tag)
    num_seats = models.IntegerField(null=True, blank=True)
    creator = models.CharField(max_length=200, null=False)

And this is what the error looks like:

TypeError: int() argument must be a string, a bytes-like object or a real number, not 'IntegerField'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/laithtahboub/Desktop/Programming/Django/events_project/manage.py", line 22, in <module>
    main()
  File "/Users/laithtahboub/Desktop/Programming/Django/events_project/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/management/__init__.py", line 425, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/management/__init__.py", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/management/base.py", line 373, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/management/base.py", line 417, in execute
    output = self.handle(*args, **options)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/management/base.py", line 90, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 253, in handle
    post_migrate_state = executor.migrate(
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/migrations/executor.py", line 126, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/migrations/executor.py", line 156, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/migrations/executor.py", line 236, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/migrations/migration.py", line 125, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/migrations/operations/fields.py", line 225, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/backends/sqlite3/schema.py", line 140, in alter_field
    super().alter_field(model, old_field, new_field, strict=strict)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/backends/base/schema.py", line 618, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type,
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/backends/sqlite3/schema.py", line 362, in _alter_field
    self._remake_table(model, alter_field=(old_field, new_field))
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/backends/sqlite3/schema.py", line 202, in _remake_table
    'default': self.quote_value(self.effective_default(new_field))
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/backends/base/schema.py", line 334, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 839, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 834, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 1824, in get_prep_value
    raise e.__class__(
TypeError: Field 'num_seats' expected a number but got <django.db.models.fields.IntegerField>.

Let me know if you need to see another file to determine the problem. I'm aware that there are many similar questions to this one on Stack Overflow, but please keep in mind that I have tried almost everything I can based on the answers of these questions, and nothing has worked yet.

CodePudding user response:

After deleting the field from the model class, you need to run python manage.py makemigrations before running python manage.py migrate

So the issue was fixed by deleting all the files inside the migrations folder except the __init__.py file. And removing all the rows from django_migrations table And re-applying fake migrations by python manage.py migrate --fake

This fixed the issue. (OP and me had a call to get the issue fixed)

  • Related