为列“id"指定的多个默认值桌子的 [英] Multiple default values specified for column "id" of the table

查看:30
本文介绍了为列“id"指定的多个默认值桌子的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前在我的笔记本电脑上运行我的网站,它的数据库是Sqlite,最近我想把它转移到DigitalOcean,我把它的数据库改成了Postgresql,但是我在迁移时遇到了一些问题.

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.4Django 1.8

Python 3.4 Django 1.8

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

我的模特

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)])

我该怎么办?

推荐答案

尝试明确指定 id 字段并将其标记为主键:

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 应该自动为这个字段创建一个序列.

Django should automatically create a sequence for this field.

可能是 User 没有明确定义主键的外键混淆了 ORM,尽管这只是一个理论.

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

这篇关于为列“id"指定的多个默认值桌子的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆