[英]Cannot avoid “ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR: default for column ”gender“ cannot be cast automatically to type integer”
[英]ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR: column "completed_at" cannot be cast automatically to type timestamp without time zone
我最初创建了一个任务 model,包括一个设置为time
类型的completed_at
字段,迁移如下:
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 创建构建时,使用heroku run rails db:migrate --app my_app_name
命令,运行 rails db:migrate --app my_app_name
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".
受这个 11 岁线程的启发,我尝试通过添加without time zone
选项来修改将completed_at
列的类型从 time 更改为 timestamp 的迁移,但它并没有解决问题:
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
选项的文档,因此上面的代码可能不正确。关于如何解决这个问题并使构建通过的任何想法?
Go 到 pgadmin 并尝试这个查询它对我有用。
*ALTER TABLE shop_orders ALTER COLUMN 约会日期时间 TYPE TIMESTAMP USING 约会日期时间::TIMESTAMP;
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.