[英]/django.db.utils.IntegrityError: NOT NULL constraint failed/ after python manage.py migrate app zero
I had some mess with migrations in production and localy. 我在生产和本地迁移时遇到了一些麻烦。 Finally the situation was that in production there were only initial migration and localy there were 8 migrations or something.
最后情况是,在生产中只有初始迁移,而且当地有8次迁移或其他东西。 So I decided to use
所以我决定使用
python manage.py migrate app zero
both in production and localy(django 1.8.7). 在生产和localy(django 1.8.7)。 In prodcution it worked but locally it raised error which didn't appear before after
makemigrations
or migrate
command. 在生产中它起作用但在本地它引发了错误,这在
makemigrations
或migrate
命令之后没有出现。
django.db.utils.IntegrityError: NOT NULL constraint failed: app_userprofile__new.phone_number
after few attempts to try different things, the error began to appear after migrate commands too. 在几次尝试尝试不同的事情之后,错误也开始出现在迁移命令之后。
The model itself : 模型本身:
class UserProfile(models.Model):
user = models.OneToOneField(User)
phone_number = models.IntegerField(null=True, blank=True, default=None)
Check your local database. 检查您的本地数据库。
This error usually happens when one or more records do not meet the NOT NULL requirement 当一个或多个记录不符合NOT NULL要求时,通常会发生此错误
UserProfile.objects.filter(phone_number=None)
You can resolve this by filling the phone_number field of the objects found 您可以通过填写找到的对象的phone_number字段来解决此问题
Or deleting objects that do not have filled phone_number 或者删除没有填充phone_number的对象
UPDATE UPDATE
Manage database changes with database migrations
can prevent this type of situation 使用
database migrations
管理数据库更改可以防止此类情况
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.