django.db.utils.programmingerror:无法施放类型的整数日期
大家好,当我从Docker部署时,我对PostgreSQL有一些问题。 错误:
django.db.utils.ProgrammingError: cannot cast type integer to date
[2022-06-01 09:06:04] LINE 1: ...a_playlist" ALTER COLUMN "year" TYPE date USING "year"::date
这是我的迁移文件:
class Migration(migrations.Migration):
dependencies = [
('multimedia', '0004_track_track'),
]
operations = [
migrations.AlterField(
model_name='playlist',
name='year',
field=models.DateField(blank=True, null=True),
),
migrations.AlterField(
model_name='track',
name='year',
field=models.DateField(blank=True, null=True),
),
]
如何解决该问题?谢谢你!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到的内容可能被解决如下:
假设您的模型是:
删除迁移
以避免混乱,我会删除您刚刚创建的迁移。
如果您熟悉Django,则可能会跳上下一步并编辑
Alterfield
参数。重命名您的字段
更改命名,例如temp_year。
然后运行
./ manage.py makemigrations
。您现在有一个包含更名字段的迁移。
添加适当的年度字段
,然后运行
./ manage.py makemigrations
。现在,您有一个包含所需数据类型的字段的迁移。
在您的最后一个
migrations.Addfield(...)
,addmigrations.runpython()
之后,将数据传输在迁移文件中。
发现一些有关Runpython的文档
在迁移类上方添加一个函数上方,让我们命名
map_to_year
:注意:请使用正确的一个迁移
运行迁移
,如果您运行迁移,请立即 日期对象。
接下来是什么?
一旦迁移通过,您应该能够删除temp_year字段并运行
makemigrations
What you encounter might be solved as follow :
Let's assume your models are :
Remove your migration
To avoid confusion, I'd remove the migration you just created.
If you are familiar with django you might jump on the next steps and edit the
AlterField
arguments.Rename your fields
Change the naming for example temp_year.
Then run
./manage.py makemigrations
.You have now a migration containing the renamed fields.
Add a proper year field
Then run
./manage.py makemigrations
.You have now a migrations containing the field with the data type you want.
Transfer your data
In the migrations file freshly created after your last
migrations.AddField(...)
, addmigrations.RunPython()
.some docs about RunPython are found here
Just above the migration class add a function, let's name it
map_to_year
:Note : change your_app_name with the correct one
Run the migrations
Now if you run the migrations it maps your integer from temp_year to a date object from year.
What's next ?
Once the migration pass you should be able to remove the temp_year field and run
makemigrations