gpt4 book ai didi

python - 似乎无法丢失此错误 : "You are trying to add a non-nullable field"

转载 作者:太空宇宙 更新时间:2023-11-04 05:58:19 24 4
gpt4 key购买 nike

无论我做什么,我似乎都无法在 django 模型中丢失此错误:

Did you rename twitterbot.bot to twitterbot.userbot (a ForeignKey)? [y/N] y
You are trying to add a non-nullable field 'twitbot_id' to twitterbot 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)
2) Quit, and let me add a default in models.py
Select an option: 2

正如您在这里看到的,我已经根据文档正确设置了它,给出了一个“自动字段”。

那么是什么导致此错误持续存在?创建 Null = True/False 似乎没有任何改变。

class TwitterBot(models.Model):
twitbot_id = models.AutoField(primary_key=True)
userbot = models.ForeignKey(UserBot)
user_id = models.CharField(max_length=90, blank=True, null=True)
screenname = models.CharField(max_length=200, blank=True, null=True)
oauth_token = models.CharField(max_length=90, blank=True, null=True)
oauth_token_secret = models.CharField(max_length=90, blank=True, null=True)
hashtags = models.CharField(max_length=200, blank=True, null=True)

最佳答案

Django 为每个模型添加一个默认的“id”字段,您不需要在模型中添加额外的“twitbot_id”。如果代理主键是您所需要的全部,请忘记“twitbot_id”,因为它将是自动生成的“id”的副本。参见 https://docs.djangoproject.com/en/dev/topics/db/models/#automatic-primary-key-fields

如果添加此项并且数据库中已有 TwitterBot 对象,则必须提供默认值来为数据库中的现有行填充此列。

关于python - 似乎无法丢失此错误 : "You are trying to add a non-nullable field",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26558261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com