You are trying to add a non-nullable field 'password' to userinfo without a default問題

當向models.py對應類添加一個新字段

password = models.CharField(max_length=20)

之後,執行python3 manage.py makemigrations命令提示以下信息

You are trying to add a non-nullable field 'password' to userinfo 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 with a null value for this column)
 2) Quit, and let me add a default in models.py

只需要在字段的後面加一個default=""即可:

password = models.CharField(max_length=20,default="")

再執行 python3 manage.py makemigrations命令

Migrations for 'grzx':
  grzx\migrations\0005_userinfo_password.py
    - Add field password to userinfo

再執行python3 manage.py migrate即可向數據庫新增字段

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, grzx, sessions
Running migrations:
  Applying grzx.0005_userinfo_password... OK





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章