Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...