数据库postgresql
我有以下迁移:
class AlterBirthdayInUsers < ActiveRecord::Migration[5.0]
def change
change_column :users, :birthday, 'date USING CAST(birthday AS date)', default: Date.today
end
end
迁移开始出错:
rake stdout: == 20170201162913 AlterBirthdayInUsers: migrating =======================
-- change_column(:users, :birthday, "date USING CAST(birthday AS date)", {:default=>Sat, 18 Feb 2017})
rake stderr: rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::InvalidDatetimeFormat: ERROR: invalid input syntax for type date: ""
: ALTER TABLE "users" ALTER COLUMN "birthday" TYPE date USING CAST(birthday AS date)
类型为date的迁移字段中postgrees的正确点是什么?
提前感谢您的回复。
答案 0 :(得分:3)
它可能无法尝试将空字符串转换为日期, 变化
USING CAST(birthday AS date)
到
USING CAST(case when birthday = '' then null else birthday end AS date)