【发布时间】:2017-06-28 16:19:42
【问题描述】:
我在models.py 中添加了一个新的 ImageField:
class User(AbstractUser):
[more_fields_here]
profile_picture = models.ImageField(upload_to='profile_pictures', null=True)
我运行python manage.py makemigrations,然后运行python manage.py migrate,没有任何错误。
但是当我运行我的应用程序时,我得到:
ProgrammingError at column authentication_user.profile_picture does not exist
我检查了 Postgres 数据库,但 profile_picture 列不存在。
我删除了迁移并再次尝试,但仍然遇到同样的错误。
在migrations/0001_initial.py 中有一行:
('profile_picture', models.ImageField(null=True, upload_to='profile_pictures')),
但是为什么表中不列存在呢?
【问题讨论】:
-
您是否手动修改了迁移文件?我怀疑 Django 认为
0001_initial已经迁移,这就是它不添加此字段的原因。我还建议尝试python manage.py migrate appname zero回滚所有迁移,然后再次运行python manage.py migrate。 -
您是否将
AUTH_USER_MODEL设置为您的自定义用户模型?您看到数据库中的[more_fields_here]字段了吗? -
@Chris,我可以确认我已经设置了
AUTH_USER_MODEL。所有其他字段都按预期在数据库中。问题只出在这个字段上。 -
manage.py sqlmigrate <app_name> 0001是否包含用于创建列的 SQL? -
@Kamil,您可以添加您的评论作为答案,以便可能有类似问题的其他人可以更仔细地找到它。
标签: django postgresql django-models