Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple default values specified for column "id" of the table

I use to run my website on my laptop and its database was Sqlite, recently I wanted to transfer it to DigitalOcean and I changed its database to Postgresql, but when I migrate I encounters with some problems.

Python 3.4 Django 1.8

Error

django.db.utils.ProgrammingError: multiple default values specified for column "id" of table "profiles_userprofile"

My Model

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    avatar = models.ImageField(blank=True, upload_to=get_image_path, default='/static/image/avatar/male.png')
    age = models.IntegerField(default=4, validators=[MinValueValidator(3), MaxValueValidator(99)])

What should I do?

like image 533
altruistic Avatar asked Jun 21 '15 13:06

altruistic


1 Answers

Try explicitly specifying the id field and marking it as the primary key:

class UserProfile(models.Model):
    id = models.BigIntegerField(primary_key = True)
    user = models.OneToOneField(User)
    avatar = models.ImageField(blank=True, upload_to=get_image_path, default='/static/image/avatar/male.png')
    age = models.IntegerField(default=4, validators=[MinValueValidator(3), MaxValueValidator(99)])

Django should automatically create a sequence for this field.

It may be that the User foreign key without an explicitly defined primary key is confusing the ORM, although that's just a theory.

like image 86
khampson Avatar answered Sep 25 '22 19:09

khampson