[Fixed]-Django migrate error : TypeError expected string or bytes-like object

11👍

If you modified the fields in models. after that you run makemigrations that time it asking like this

^C(api_env)nyros@nyros:~/Desktop/santhi_projects/sample_api/sample_api$ python manage.py makemigrations
You are trying to add a non-nullable field 'provider' to content without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py
Select an option: 1

We select 1 option, then it will display like this

Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()
>>> timezone.now()

We given timezone.now() then makemigrations completed.

See your migrations last file, there is

class Migration(migrations.Migration):

dependencies = [
    migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ('providers', '0023_remove_content_provider'),
]

operations = [
    migrations.AddField(
        model_name='content',
        name='provider',
        field=models.ForeignKey(related_name='library', default=datetime.datetime(2016, 11, 1, 7, 15, 12, 655838, tzinfo=utc), to=settings.AUTH_USER_MODEL),
        preserve_default=False,
    ),
]

In above code observe this line

default=datetime.datetime(2016, 11, 1, 7, 15, 12, 655838, tzinfo=utc)

In that line forgeinkey field default value is datetime, Is it correct value?

No, so you have to give some string or object is a default value to that field.

Now you have to edit that value in that corresponding migration file, like this

default='some string'

then save and run the migrate command.

Try and let me know, Is it works or not.Thanks

5👍

Change your model field default value for auto_now_add=True

end_date = models.DateTimeField(auto_now_add=True)
start_date = models.DateTimeField(auto_now_add=True)
👤levi

4👍

Remove your migrations with these commands :

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete

Then delete database file with rm db.sqlite

Then run your server

3👍

Goto migration folder “migrations

go to your migration file e.g"0054_comment_date.py"

change the 'default' in line field=models.DateTimeField(auto_now_add=True, default=0)

from 0 or whatevere is there

to default='2012-09-04 06:00:00.000000-08:00

2👍

Delete all the migrations manually. and then type
python manage.py makemigrations and python manage.py migrate

— Hope this may work .. worked for me.

👤Suceel

1👍

Just remove all files in migrations folder except “init.py”
and delete “db.sqlite3” i.e your database and now make changes that you want and migrate.Hope This helps 🙂

1👍

I encountered with the same problem. The solution was to delete all default spaces in ‘migrations’ folder.

while you type ‘python manage.py migrate’ go up in terminal and find where the exact file in ‘migrations’ stopping the migrating. Then find all default arguments for DateField or DateTimeField and delete it. Those models don’t handle with default argument.

migrations.AlterField(
        model_name='order',
        name='start_date',
        field=models.DateField(auto_now_add=True, ***BUG default=False BUG***),
        preserve_default=False,
    ),

See the log in your terminal:

Running migrations:
  Applying ang.0003_poll_end_date...Traceback (most recent call last):

in this file there is a bug. Go there and delete the default argument from DateField

1👍

You can just use end_date = models.DateTimeField(auto_now_add=True) and it automatically creates a default date for you.
If this error not solved with this, then try deleting all your migrations. After this again use python manage.py makemigrations and python manage.py makemigrations <app_name>, then migrate, it should definitely work.

I followed this and this worked for me

0👍

Same issue here, when running through adding placeholders to my datefields, I accidentally entered “1” where “django.utils.timezone.now” should be, that is for the date-time default. Go into your last migration and find the “default=xxx” that is not like the rest and insert “django.utils.timezone.now”

0👍

As a workaround may run ./manage.py migrate –fake

0👍

In case all the above solutions do not work for you, like in my case. You can easy solve it with these simple steps.

  1. Go into your apps directory and delete all previous migrations leaving only the

pycache
init.py

  1. Go into your main project directory and delete the database. (NB: no side effect, please just do it)

  2. With all done, open your terminal and run
    $ python manage.py makemigrations
    $ python manage.py migrate

Done. Thanks.

0👍

Just running:

python manage.py makemigrations 

and

python manage.py migrate

worked for me, I refer Suceel answer 👍

0👍

If you follow the instructions below you’ll be good to have your error fixed.

  1. datetime = models.DateTimeField(auto_now_add=True)
  2. you will be provided with two options in which you have to choose option >> 1 Provide the default…
  3. and then type timezone.now
  4. and finally press enter

0👍

I just deleted all my migrations and redid it. made it zero and re-migrated it all

-2👍

I had the same problem. Look at all the models.DateTimeField lines in the .py file generated after calling makemigrations. There you will notice that some of the DateTimeField have a wrong default value. Replace those with models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now) for example.

Leave a comment