gpt4 book ai didi

Django 不会在 Postgres 上创建 ImageField

转载 作者:行者123 更新时间:2023-11-29 11:49:25 25 4
gpt4 key购买 nike

我在我的 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 之后添加的。

解决此问题的最简单方法(无需回滚任何迁移):

  1. 0001 迁移中删除字段 profile_picture
  2. 再次运行 makemigrations(应创建带有新字段 profile_picture0002)
  3. 运行迁移

关于Django 不会在 Postgres 上创建 ImageField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44807845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com