2017-02-22 95 views
0

數據庫PostgreSQL的PG :: InvalidDatetimeFormat:錯誤:類型日期無效的輸入語法:「」

我有以下遷移:

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

嘗試改變'使用CAST(生日AS日期)'來'使用CAST(情況下,當生日=''然後null其他生日結束AS日期)' –

+0

謝謝你,尊重 –

回答

3

但是卻沒有嘗試空字符串轉換爲日期, 變化

USING CAST(birthday AS date) 

USING CAST(case when birthday = '' then null else birthday end AS date) 
相關問題