IntegrityError:列“city_id"中的空值;违反非空约束 [英] IntegrityError: null value in column "city_id " violates not-null constraint
本文介绍了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)
当我同步数据库并创建管理员用户时:
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
就会失败.
null, blank
because the sycndb
will create a profile and will fail if no city
is passed.
或者,您可以将 default=default_city_id
传递给 city
ForeignKey
Alternatively, you can pass default=default_city_id
to the city
ForeignKey
这篇关于IntegrityError:列“city_id"中的空值;违反非空约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文