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

Ste*_*sch 1 postgresql ruby-on-rails

数据库 postgresql

我有以下迁移:

class AlterBirthdayInUsers < ActiveRecord::Migration[5.0]
  def change
    change_column :users, :birthday, 'date USING CAST(birthday AS date)', default: Date.today
  end
end
Run Code Online (Sandbox Code Playgroud)

迁移开始出错:

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)
Run Code Online (Sandbox Code Playgroud)

类型日期的迁移字段中 postgrees 的正确点如何?

预先感谢您的回复。

Vao*_*sun 5

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

USING CAST(birthday AS date) 
Run Code Online (Sandbox Code Playgroud)

USING CAST(case when birthday = '' then null else birthday end AS date)
Run Code Online (Sandbox Code Playgroud)