Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...