** "It looks good with date
instead of this column datetime
" **
I used datetime
because I deal with dates, but when I think about it carefully, I don't need" time ", so I want to convert it to date
.
--Book models have a release date released_at (datetime)
--I want to migrate and make it released_on (date)
def change
rename_column :books, :released_at, :released_on
change_column :books, :released_on, 'date USING CAST(released_on AS date)'
end
db: rollback
?That's okay, just wait a minute with git commi ..
, **! ** **
Keep in mind that migrations roll back.
Let's see if we can roll back the migration mentioned above.
$ rails db:rollback
== ..... : reverting ========
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
This migration uses change_column, which is not automatically reversible.
To make the migration reversible you can either:
1. Define #up and #down methods in place of the #change method.
2. Use the #reversible method to define reversible behavior.
--When you roll back with change_column
, you can't restore it because you don't know the previous type information.
I was able to convert the type with 'date USING CAST (released_on AS date)'
, but the rollback failed because ** "type information before conversion" ** was missing.
As one way to solve this, let's rewrite it as ** up / down method **.
def up
rename_column :books, :published_at, :release_on
change_column :books, :release_on, 'date USING CAST(release_on AS date)'
end
def down
rename_column :books, :release_on, :published_at
change_column :books, :published_at, 'timestamp USING CAST(published_at AS timestamp)'
end
By doing this
--db: migrate
executes datetime-> date
of ʻup --
db: rollback executes
down
date-> datetime`
datetime USING CAST
gives an error in PostgreSQLchange_column :books, :released_at, 'datetime USING CAST(released_at AS datetime)'
PG::UndefinedObject: ERROR: type "datetime" does not exist
LINE 1: ...released_at" TYPE datetime USING CAST(published_at AS datetime)
It was said that datetime
in the Rails world becomestimestamp
in the PostgreSQL world: sweat_smile:
Recommended Posts