Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django不会在Postgres上创建ImageField_Django_Postgresql_Django Models - Fatal编程技术网

Django不会在Postgres上创建ImageField

Django不会在Postgres上创建ImageField,django,postgresql,django-models,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没有任何错误 但当我运行我的应用程序时,我得到:

我在我的
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\u picture
列不存在

我删除了迁移并重试,但仍然收到相同的错误

migrations/0001_initial.py
中有一行:

('profile_picture', models.ImageField(null=True, upload_to='profile_pictures')),

但是为什么表中不存在列呢?

看起来迁移有问题,不建议手动修改迁移文件。Django存储已应用迁移的信息,如果修改已应用的
0001
迁移并再次运行
migrate
,则不会应用这些修改。当然,我不知道这是否确实发生在您身上,但它看起来像是在应用
0001
后添加的
profile\u picture
字段

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

  • 0001
    迁移中删除字段
    profile\u picture
  • 再次运行
    makemigrations
    0002
    ,应创建新字段
    profile\u picture
  • 运行
    migrate

  • 您是否手动修改了迁移文件?我怀疑Django认为
    0001_initial
    已经迁移,这就是为什么它没有添加此字段。我还建议尝试
    python manage.py migrate appname zero
    回滚所有迁移,然后再次运行
    python manage.py migrate
    。您是否设置了自定义用户模型?你看到数据库中的
    [更多字段]
    字段了吗?@Chris,我可以确认我已经设置了
    AUTH\u USER\u模型
    。所有其他字段都如预期的那样位于数据库中。问题仅限于此字段。
    manage.py sqlmigrate 0001
    是否包含SQL以创建列?@Kamil,您可以添加注释作为答案,以便其他可能有类似问题的人可以更仔细地查找。