IntegrityError:列“city_id”中的空值违反非空限制 [英] IntegrityError: null value in column "city_id " violates not-null constraint

查看:147
本文介绍了IntegrityError:列“city_id”中的空值违反非空限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的两个模型:

class City(models.Model):
    name = models.CharField(max_length=50)
    country = models.OneToOneField(Country)

    def __unicode__(self):
        return self.name 


class UserProfile(models.Model):
    user = models.OneToOneField(User)
    city = models.OneToOneField(City)

当我syncdb并创建管理员用户:

when I syncdb and create admin user :

IntegrityError: null value in column "city_id" violates not-null constraint

我如何修复此错误?

推荐答案

city = models.OneToOneField(City)

您确定每个城市只想要一个用户吗?我想你需要

Are you sure you want only one user per city? I think you need

city = models.ForeignKey(City, null=True, blank=True)

null,blank 因为 sycndb 将创建一个配置文件,如果没有 city 通过,将失败。

或者,您可以通过 default = default_city_id 城市 ForeignKey

Alternatively, you can pass default=default_city_id to the city ForeignKey

这篇关于IntegrityError:列“city_id”中的空值违反非空限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆