Ruby/Rails - Active Record Db Migration to MySQL - timestamp type?
You can always insert a column with a custom type if you want. The symbol names are automatically converted into whatever ActiveRecord defines, but if you use a plain string it goes in as-is:
t.column :ar_timestamp, :timestamp
t.column :mysql_timestamp, 'timestamp'
What you get is ar_timestamp
being the usual DATETIME
type where mysql_timestamp
is defined as TIMESTAMP
.
Preventing Rails 4 migration from changing timestamp to datetime?
Well, As far as ActiveRecord is concerned timestamps == datetime
. so when you call t.timestamp
it created an column with type as datetime
. AR doesn't seem to use MYSQL's TIMESTAMP datatype. here is the relavent source.
def add_timestamps(table_name)
add_column table_name, :created_at, :datetime
add_column table_name, :updated_at, :datetime
end
As you may note above, rails is creating two columns with datetime
datatype instead of timestamp. so you will have explicitly specify the column type like this:
t.column, :timestamp, "column_name", limit: 6
Converting mysql column with type of datetime to time using Rails migration
Ensure you've run the migration.
Because, if the MySQL type is DATETIME
of TIMESTAMP
, Rails represents is DateTime
. But for DATE
and TIME
, Rails represents it as-is: Date and Time. Only for the DATETIME
/TIMESTAMP
does Rails take an auto-decision for us. For the rest, it maintains as-is.
Rails' datetime and timestamp in a migration file are the same for MySQL and Sqlite3?
I would recommend using the :datetime
, because so it's clear what to use. I believe rails is using DATETIME
for both in the DB is because of the Problem that the datetimes representable with the unix timestamp or the MySQL TIMESTAMP
field. Since timestamp is by default a 32bit Integer (see Wikipedia:Timestamp) it can only represent dates between 1901-12-13
(or 1970-01-01
if no negative values are allowed like in MySQL) and 2038-01-19
. After or before that it will overflow. This is the year 2038 problem.
So to make it clear to everybody I would name it :datetime
in the migration.
The TIMESTAMP data type has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. It has varying properties, depending on the MySQL version and the SQL mode the server is running in. These properties are described later in this section. Source
Change a column type from Date to DateTime during ROR migration
First in your terminal:
rails g migration change_date_format_in_my_table
Then in your migration file:
For Rails >= 3.2:
class ChangeDateFormatInMyTable < ActiveRecord::Migration
def up
change_column :my_table, :my_column, :datetime
end
def down
change_column :my_table, :my_column, :date
end
end
ActiveRecord DATETIME column values in mysql
It seems like there is no way built in to Rails to get around casting that date to something non-nil.
The fact that there is no way around this is a sign that you're probably doing something in a non-optimal way, so my first suggestion is to just migrate all 0000-00-00 00:00:00
values to nil
if you can.
If that isn't an option. You can find out whether any one record has 0000-00-00 00:00:00
via sql conditions something like this:
Post.where("date_field = '0000-00-00 00:00:00' && id = ?", post.id).exists?
Hope that helps.
Related Topics
How to Condense Summable Metrics to a Unique Identifier in a Ruby Table
How to Escape All Characters with Special Meaning in Regex
When Passing a Ruby Array as an Argument, Why Does '<<' Append While '+=' Does Not
Devise 'Find_First_By_Auth_Conditions' Method Explanation
How to Access Ruby Method Inside Module and Inside a Class from Another .Rb File
Nomethoderror: Undefined Method '[]' for #<Activerecord::Migration:0X000000033Fedc0>
How Does Rails Know My Timezone
When Trying to Run Rspec, I Get "Uninitialized Constant Activemodel"
How to Output Leading Zeros in Ruby
Sorting an Array of Objects in Ruby by Object Attribute
Instagram Ruby Gem - Unable to Reach Callback Url
Cannot Install Ruby 1.9.3 on a Clean Lion Install
How to Use Link_To with Ajax in Rails 3.2.1
Ruby Selenium Web Drive: How to Find Specific Element by Xpath Div Id and CSS Class
Ruby on Rails View Rendering Db Info on Page