django-问题集

django相关问题

orm相关问题

1.编写orm在数据库中添加字段,遇到以下内容

(.venv) E:\document\py_object\fun_object\wuruntao>python manage.py makemigrations
It is impossible to add a non-nullable field 'phone' to user without specifying a default. This is because 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 manually define a default value in models.py.

这个错误信息的意思是:在尝试添加一个非空的字段 phone 到 user 表时,如果该表已经存在数据,数据库需要一个默认值来填充现有的记录。因为现有的记录可能没有 phone 字段的值,所以需要一个默认值来填充这个字段。
有两种解决方案

  • 提供一个临时的默认值:
    1.输入1
    2.输入临时的默认值【给已有记录填充】
    
  • 在models中定义默认值【default参数】
    修改orm
    1.phone = models.CharField(verbose_name="手机号", max_length=64, 
    default="1234567890")  
    2.执行 python manage.py makemigrations
    3.执行 python manage.py migrate
    
posted @ 2024-07-08 12:14  清风拂山岗(小高同学)  阅读(29)  评论(0)    收藏  举报