Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I two model:

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

How I can Fix this error?

like image 582
Vahid Kharazi Avatar asked May 17 '13 07:05

Vahid Kharazi


1 Answers

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 because the sycndb will create a profile and will fail if no city is passed.

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

like image 163
tuxcanfly Avatar answered Nov 01 '22 04:11

tuxcanfly