Set wrong default value in Django model
So I added a new TimeField to my django model. When I did "python3 manage.py makemigrations" it asked me to choose two options, set a default value now or add one another way. I chose to set one now. I wasn't thinking and set it to "" which is incorrect format for a time field. I have tried everything, even manually in sqlite, removing the new field and more, but I still get an error when running "python3 manage.py migrate" which is saying this:
django.core.exceptions.ValidationError: ['“” value has an invalid format. It must be in HH:MM[:ss[.uuuuuu]] format.']
If someone can help. me, it will be gladly appreciated!
In order to revert changes, you can perform two different actions:
Manually changing the migration produced by the execution of this command
Manually removing that migration file
Probably, you should also update the interested database tables, depending on your situation.
You can try using this command -
python manage.py migrate <app_name> <previous_migration>
At the place of previous_migration insert the number of migration, like 0004 if 0005 is the number of migration file in which you have set the default value.
This will revert all the change till 0004 migration files and 0004 migration changes will be present in the databases.
After that you can change the default value from the models.
For more details - https://docs.djangoproject.com/en/5.1/topics/migrations/#reversing-migrations
Have you checked the migration directory?
- When you make changes to your data base and these changes are typically tracked in this directory.
In these files look for the added default section or something similar to
migrations.AlterField
here you can check what the default value is. The script may be reading this migration file raising the error before the change to the proper default. I have ran into this issue previously, you can either manually change the values or delete the whole file and run it again.
Source: Previously had this issue.
You can try to delete the faulty migration manually.
First, remove the migration that added the field with "". Navigate to your app's migrations folder (your_app/migrations/) and delete the last migration file (do not delete _init_.py).
Reset the migrations in Django runnig on your console:
python3 manage.py makemigrations your_app
If the TimeField needs a default value, use a valid format like 00:00:00:
from django.db import models
class YourModel(models.Model):
your_time_field = models.TimeField(default="00:00:00") # Valid value
Or, if you want it to be optional:
your_time_field = models.TimeField(null=True, blank=True)
Finally Apply the migrations again:
python3 manage.py migrate your_app
Remember to change ´your_app´ for the name of your App
You can roll back to the migration just before the incorrect TimeField default was set using:
python manage.py migrate <app_name> <previous_migration_number>
To find the migration number, run:
ls <app_name>/migrations/
Then, pick the last migration before the incorrect one and use:
python manage.py migrate <app_name> <that_migration>
Once rolled back, edit the migration file (<app_name>/migrations/xxxx_auto_*.py), and update the TimeField default correctly (or remove it if not needed).
After fixing it, re-run:
python manage.py makemigrations <app_name>
python manage.py migrate <app_name>