PG :: InvalidDatetimeFormat:错误:类型为date的输入语法无效:“”

时间:2017-02-22 12:52:53

标签: ruby-on-rails postgresql

数据库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的正确点是什么?

提前感谢您的回复。

1 个答案:

答案 0 :(得分:3)

它可能无法尝试将空字符串转换为日期, 变化

USING CAST(birthday AS date) 

USING CAST(case when birthday = '' then null else birthday end AS date)