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,您可以添加注释作为答案,以便其他可能有类似问题的人可以更仔细地查找。