python3 manage.py migrate gives error about field even when it is deleted from the model class
Asked Answered
H

1

3

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.

Hilbert answered 7/3, 2022 at 18:36 Comment(2)
I don't understand your comment "Even after deleting the field, the same error occurs". The error refers to num_seats, which you clearly have not deleted.Spearing
Sorry for not clarifying that. What I meant was that I've tried to delete it and run the command, but I get the exact same error.Hilbert
M
2

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)

Morty answered 7/3, 2022 at 18:59 Comment(9)
There is another way to fix this too. If you can show the contents of your migrations file (the file that is in the migrations folder of your Django app which has the code corresponding to this model), that would help.Morty
I know. It doesn't work.Hilbert
What error are you getting when you run python manage.py makemigrations ?Morty
I don't get an error when running python3 manage.py makemigrations. Only when I run python3 manage.py migrateHilbert
Can you show the contents of the migration file? Also a snap shot of the data in your django_migrations table?Morty
Which file in the migrations folder is the migration file?Hilbert
Thank you so much @Arun T. I'm sure not a lot of people are willing to meet on Zoom to help users on Stack Overflow. You're the best!Hilbert
Nah bro, Don't mention it !!! I know the feeling of working with Django migrations, Been through it alone, So I know the feeling. :DMorty
I had to comment out the model that gave me this error and the OneToOneField. Since I also added the model in admin.py, I comment that one out as well. After commenting the model out I did manage.py migrate --fake. After the fake migration I added back the model and OneToOneField, and the error was gone.Beach

© 2022 - 2024 — McMap. All rights reserved.