Django 不会在 Postgres 上创建 ImageField

标签 django postgresql django-models

我在我的 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/

相关文章:

python - 这两种运行 Django 的配置有什么区别?

django - SWFUpload 与 Django 1.2 csrf 问题

django - 访问 Django 中介模型值

ruby-on-rails - 避免 Rails 4 中的 Float 截断

sql - 如何重写在行中给出特定值数量的查询以避免某些值并进一步计算其他值?

python - Django Firebase

postgresql - 使用 pgadmin 远程访问 postgresql

Django runserver 给了我语法错误

Django - 如何在 'NOT IN' 中指定 'limit_choices_to' ?

Django Migrate - 行具有无效的外键,但行不存在