Forum: Ruby on Rails migrations :timestamp becomes :datetime in mySql

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
44b774df11dd1d83bab14d086b355c40?d=identicon&s=25 Peter Gd (accident)
on 2006-05-10 11:28
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 ;
Eeb0d7ac5bb9434b3335443dd6fa4523?d=identicon&s=25 Tom ten Thij (Guest)
on 2006-05-10 12:11
> 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: http://wiki.rubyonrails.com/rails/pages/MagicFieldNames).

Cheers Tom.
631d1677786f0725da6ccee46043686e?d=identicon&s=25 Jonathan Viney (Guest)
on 2006-05-10 12:13
(Received via mailing list)
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.
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-05-10 13:14
(Received via mailing list)
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/Ti...

--

Ray
This topic is locked and can not be replied to.