问题描述
我想使用 FluentMigrator 将类型为 time 的列更改为时间戳 这是我的代码
this.Alter.Table(Tables.Events.Name)
.InSchema(Tables.DefaultSchemaName)
.AlterColumn(Tables.Events.ColumnsCreatedTime.Name).AsDateTime();
但它给了我一个错误错误是“42804:列“CreatedTime”无法自动转换为没有时区的时间戳”。
解决方法
你可以尝试用 CreatedTime 创建一个新的 DateTime
new DateTime(CreatedTime);
,
我怀疑这是问题所在 - 以及解决方案:
column "date" cannot be cast automatically to type timestamp with time zone django/postgres
错误:
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".
解决方案:如果您的列无法从新列的原始类型完全转换,请考虑删除旧列并 创建一个新的:
- 注释掉模型中的列字段
- makemigration(将为此字段生成删除迁移)-> 您将删除所有列数据
- migrate 在模型中留下评论
- 再次运行 makemigration(现在将使用新数据类型的新列生成新迁移)
- 再次运行 migrate 以应用更改 如果您的字段很少,这很有效,但如果您有许多要转换的字段,我建议您使用 为此编写特定的数据迁移
这是另一种可能的解决方案(相同的 thread):
ALTER TABLE table_name ALTER COLUMN col_name TYPE timestamp with time zone USING col_name::timestamp with time zone
您的环境和另一个线程之间存在差异(例如,您使用的是 C#,另一个线程使用的是 Python) - 但我相信这是相同的潜在问题。
问:你的数据库是什么? PostgreSQL,还是“别的东西”?