无论我做什么,我似乎都无法在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
正如您在此处所见,我已根据文档对其进行了正确设置,并提供了" autofield"。
那么导致此错误持续的原因是什么?创建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)
答案 0 :(得分:1)
Django添加了默认" id"对于每个模型,你不需要额外的" twitbot_id"在你的模型中。如果您需要代理主键,请忘记" twitbot_id"因为它将与自动生成的" id"重复。见https://docs.djangoproject.com/en/dev/topics/db/models/#automatic-primary-key-fields
如果添加此项并且数据库中已有TwitterBot对象,则必须提供一个默认值,以便为数据库中的现有行填充此列。