简体   繁体   中英

PG::InvalidDatetimeFormat: ERROR: invalid input syntax for type date: “”

Database postgresql

I have following migration:

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

Migration is started an error:

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)

How correct point for postgrees in the migration field of type date?

Thanks in advance for your reply.

It probably fails trying to cast empty string to date, change

USING CAST(birthday AS date) 

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM