You are trying to add a non-nullable field 'new_field' to userprofile without a default

前端 未结 16 1309
北恋
北恋 2020-11-30 19:27

I know that from Django 1.7 I don\'t need to use South or any other migration system, so I am just using simple command python manage.py makemigrations

相关标签:
16条回答
  • 2020-11-30 20:19

    You can't add reference to table that have already data inside.
    Change:

    user = models.OneToOneField(User)
    

    to:

    user = models.OneToOneField(User, default = "")
    

    do:

    python manage.py makemigrations
    python manage.py migrate
    

    change again:

    user = models.OneToOneField(User)
    

    do migration again:

    python manage.py makemigrations
    python manage.py migrate
    
    0 讨论(0)
  • 2020-11-30 20:20

    If you are in early development cycle and don't care about your current database data you can just remove it and then migrate. But first you need to clean migrations dir and remove its rows from table (django_migrations)

    rm  your_app/migrations/*
    
    rm db.sqlite3
    python manage.py makemigrations
    python manage.py migrate
    
    0 讨论(0)
  • 2020-11-30 20:20

    If you are early into the development cycle you can try this -

    Remove/comment that model and all its usages. Apply migrations. That would delete that model and then add the model again, run migrations and you have a clean model with the new field added.

    0 讨论(0)
  • 2020-11-30 20:24

    Do you already have database entries in the table UserProfile? If so, when you add new columns the DB doesn't know what to set it to because it can't be NULL. Therefore it asks you what you want to set those fields in the column new_fields to. I had to delete all the rows from this table to solve the problem.

    (I know this was answered some time ago, but I just ran into this problem and this was my solution. Hopefully it will help anyone new that sees this)

    0 讨论(0)
提交回复
热议问题