我在我的 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
迁移并再次运行 migrate
,则不会应用这些修改。当然,我不知道这是否正是您遇到的情况,但看起来 profile_picture
字段是在应用 0001
之后添加的。
解决此问题的最简单方法(无需回滚任何迁移):
- 从
0001
迁移中删除字段profile_picture
- 再次运行
makemigrations
(应创建带有新字段profile_picture
的0002
) - 运行
迁移
关于Django 不会在 Postgres 上创建 ImageField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44807845/