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





猜你喜欢

转载自blog.csdn.net/qq_21205435/article/details/78894949