2013-05-17 22 views
7

I 2モデル:私はsyncdbの実行と管理者ユーザーを作成するときにIntegrityError:コラム "CITY_ID" にNULL値は非NULL制約に違反する

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) 

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

私はこのエラーを修正するにはどうすればよいですか?

答えて

8
city = models.OneToOneField(City) 

市には1人のユーザーしかいませんか?私はsycndbは、プロファイルを作成し、何cityが渡されない場合に失敗しますので、あなたが

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

null, blank必要があると思います。

また、あなたはcityForeignKey

default=default_city_idを渡すことができます
関連する問題