我最初创建了一个任务模型,包括设置为
completed_at
类型的 time
字段,并进行以下迁移:
class CreateTasks < ActiveRecord::Migration[7.0]
def change
create_table :tasks do |t|
...
t.time :completed_at
...
t.timestamps
end
end
end
随着项目的发展,我将
completed_at
字段转换为 timestamp
列,迁移如下:
class ChangeCompletedAtToBeTimestampInTasks < ActiveRecord::Migration[7.0]
def change
change_column :tasks, :completed_at, :timestamp
end
end
该应用程序使用 SQLite3 在本地运行良好,但是当我尝试使用 PostgreSQL 使用
heroku run rails db:migrate --app my_app_name
命令为 Heroku 创建构建时,我遇到了以下错误:
INFO -- : Migrating to ChangeCompletedAtToBeTimestampInTasks (20220713141025)
== 20220713141025 ChangeCompletedAtToBeTimestampInTasks: migrating ============
-- change_column(:tasks, :completed_at, :timestamp)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::DatatypeMismatch: ERROR: column "completed_at" cannot be cast automatically to type timestamp without time zone
HINT: You might need to specify "USING completed_at::timestamp without time zone".
加:
Caused by:
ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR: column "completed_at" cannot be cast automatically to type timestamp without time zone
HINT: You might need to specify "USING completed_at::timestamp without time zone".
并且:
Caused by:
PG::DatatypeMismatch: ERROR: column "completed_at" cannot be cast automatically to type timestamp without time zone
HINT: You might need to specify "USING completed_at::timestamp without time zone".
受到this 11年前的帖子的启发,我尝试通过添加
completed_at
选项来修改将without time zone
列的类型从时间更改为时间戳的迁移,但它没有解决问题:
class ChangeCompletedAtToBeTimestampInTasks < ActiveRecord::Migration[7.0]
def change
change_column :tasks, :completed_at, :timestamp without time zone
end
end
值得一提的是:
timestamp
列设置为 with time zone
或 without time zone
。without time zone
选项的文档,因此上面的代码可能不正确。关于如何解决此问题并使构建通过有什么想法吗?
转到 pgadmin 并尝试这个查询,它对我有用。
ALTER TABLE shop_orders ALTER COLUMN appointment_datetime TYPE TIMESTAMP USING appointment_datetime::TIMESTAMP;
谢谢
-- 添加“状态”列,默认值为“待处理” 更改表命令 添加列状态 VARCHAR(10) 默认“待处理”;
-- 如果“completed_at”不为空,则将“status”列更新为“COMPLETE” 更新订单 设置状态=“完成” WHERE Completed_at IS NOT NULL;