Django数据库迁移时报错:You are trying to add a non-nullable field ‘pwd‘ to busin without a default

 数据库第一次迁移完成之后,向表中再添加新的字段,生成迁移文件会报错:You are trying to add a non-nullable field 'pwd' to busin without a default; we can't do that (the database needs something to populate existing rows).

 大致意思是:您正在添加一个不为空的字段到某个模型类中,而模型类中没有默认值;我们不能这样做,数据库需要一些东西来填充现有的行

解决方法:

先给字段设置一个默认值或允许为空

eg:pwd = models.CharField(max_length=200,null=True)

生成迁移文件:python manage.py makemigrations

执行迁移文件:python manage.py migrate

然后再把设置的默认值和或允许为空删掉

即:pwd = models.CharField(max_length=200)

执行生成迁移文件:python manage.py makemigrations

再执行迁移文件:python manage.py migrate

ok!

还有一个简单的方法就是删库重来

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
It seems like you are facing a database migration issue in Django. In this case, you have added a non-nullable field 'Number' to the 'author' model without a default value, and there are existing rows with null values for this column. To solve this issue, you have two options: 1. Provide a one-off default now: You can provide a default value for the 'Number' field that will be set on all existing rows with a null value for this column. You can do this by running the following command in your Django project directory: ``` python manage.py makemigrations --empty yourappname ``` This will create an empty migration file for your app. Then add the following code to the `operations` list in the `migrations.RunPython` method: ``` from django.db import migrations def set_default_value(apps, schema_editor): Author = apps.get_model('yourappname', 'Author') Author.objects.filter(Number=None).update(Number=0) class Migration(migrations.Migration): dependencies = [ ('yourappname', 'previous_migration'), ] operations = [ migrations.RunPython(set_default_value), ] ``` Replace 'yourappname' and 'previous_migration' with your actual app name and the previous migration name respectively. Then run the following command: ``` python manage.py migrate ``` This will apply the migration and set the default value for the 'Number' field on all existing rows with null values. 2. Quit, and let me add a default in models.py: If you don't want to provide a default value for the 'Number' field, you can quit the migration process and add a default value in the 'author' model in your models.py file. You can do this by adding the following code to your model: ``` class Author(models.Model): name = models.CharField(max_length=100) # Add default value for Number field Number = models.IntegerField(default=0) ``` Then run the following command to create a new migration: ``` python manage.py makemigrations ``` This will create a new migration file with the default value for the 'Number' field. Then run the following command to apply the migration: ``` python manage.py migrate ``` This will apply the migration and add the default value for the 'Number' field in the 'author' model.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值