For some reason whenever I try and create a timestamp column with
migrations and mysql I get a datetime column instead. That’s kind of
annoying because I want the column to update every time the row gets
changed. Is this a bug, or is there something I can do about it?
(Obviously I can manually change my mysql table, but that kind of
defeats the point of migrations!)
In my migration file:
create_table :some_table do |table|
...
table.column :alter_date, :timestamp
table.column :create_date, :datetime
end
But in mysql I get:
CREATE TABLE some_table
(
…
alter_date
datetime default NULL,
create_date
datetime default NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci ;
For some reason whenever I try and create a timestamp column with
migrations and mysql I get a datetime column instead. That’s kind of
annoying because I want the column to update every time the row gets
changed.
This is desired behaviour that allows for database neutrality (see
http://torch.cs.dal.ca/~ssmith/?p=41). If you rename the column to
updated_at rails will automagically do exactly what you want (see magic
field names: Peak Obsession).
Cheers Tom.
I ran into this last week. It looks like a bug in mysql_adapter.rb.
Just changing line 106 to read:
:timestamp => { :name => “timestamp” },
may be enough to fix it, but I couldn’t be sure. Have a go.
-Jonathan.
peter gd wrote:
For some reason whenever I try and create a timestamp column with
migrations and mysql I get a datetime column instead. That’s kind of
annoying because I want the column to update every time the row gets
changed. Is this a bug, or is there something I can do about it?
There was some reason for not supporting timestamp in migrations. This
is documented somewhere, perhaps just in a blog post. A little Googling
around ought to find it.
You can use the magic field names created_on/created_at and
updated_on/updated_at to get what you want. These fields are
automatically entered with the correct value whenever you create or
update a record.
http://api.rubyonrails.com/classes/ActiveRecord/Timestamp.html
–
Ray