最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Accidentally set a wrong default value in Django model - Stack Overflow

programmeradmin4浏览0评论

So, I added a new TimeField to my Django model. When I did python3 manage.py make migrations, 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 an 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!

So, I added a new TimeField to my Django model. When I did python3 manage.py make migrations, 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 an 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!

Share Improve this question edited Mar 11 at 22:21 President James K. Polk 42.1k29 gold badges109 silver badges145 bronze badges asked Mar 11 at 15:54 Just VerhagenJust Verhagen 516 bronze badges 3
  • 1 Have you tried manually removing or updating the migration file you the command you ran have generated? – daqh Commented Mar 11 at 16:27
  • @daqh i removed them and now it works – Just Verhagen Commented Mar 11 at 16:54
  • Nice, I've posted the solution in order to help other people who are stuck with this problem. – daqh Commented Mar 11 at 17:28
Add a comment  | 

5 Answers 5

Reset to default 1

In order to revert changes, you can perform two different actions:

  1. Manually changing the migration produced by the execution of this command

  2. Manually removing that migration file

Probably, you should also update the interested database tables, depending on your situation.

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 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/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 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>
发布评论

评论列表(0)

  1. 暂无评论