列“日期”无法自动转换为带有时区的时间戳 django/postgres

5 python django postgresql date

date当我之前将日期保存为字符串时,有一个charfield 。但现在我将字段从 charfield 更改为 datetimefield 并删除了所有以前的数据。

date = models.DateTimeField(default=timezone.now)
Run Code Online (Sandbox Code Playgroud)

在本地它工作正常,但在生产中它在尝试迁移时会引发以下错误。我删除了列,模型,注释和取消注释,删除并再次创建该字段,返回到以前的迁移并再次迁移,但所有这些都没有帮助。

    django.db.utils.ProgrammingError: column "date" cannot be cast automatically to type timestamp with time zone
HINT:  You might need to specify "USING date::timestamp with time zone".
Run Code Online (Sandbox Code Playgroud)

如何正确解决问题?使用 django 1.8、python 2.7 和 postgres

Sei*_*Sys 7

对于任何在尝试将整数列转换为时间戳时在 Google 上发现此问题的人:

ALTER TABLE my_table
ALTER COLUMN my_column
TYPE timestamp with time zone
USING to_timestamp(my_column);
Run Code Online (Sandbox Code Playgroud)


moh*_*mad 2

在 postgres shell 中运行:

ALTER TABLE table_name ALTER COLUMN col_name TYPE timestamp with time zone USING col_name::timestamp with time zone
Run Code Online (Sandbox Code Playgroud)