# Migrations :timestamp becomes :datetime in mySql

**URL:** <https://www.ruby-forum.com/t/migrations-timestamp-becomes-datetime-in-mysql/60109>\
**Category:** Rails\
**Created:** [May 10, 2006, 11:28am UTC](https://www.ruby-forum.com/t/migrations-timestamp-becomes-datetime-in-mysql/60109 "2006-05-10T11:28:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgohman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/51bf81/32.png) [@pgohman](https://www.ruby-forum.com/u/pgohman)\
**Post date:** [May 10, 2006, 11:28am UTC](https://www.ruby-forum.com/t/migrations-timestamp-becomes-datetime-in-mysql/60109/1 "2006-05-10T11:28:08Z")

</div>

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 ;

---

<div class="post-metadata">

**Author:** ![pgohman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/51bf81/32.png) [@pgohman](https://www.ruby-forum.com/u/pgohman)\
**Post date:** [May 10, 2006, 12:11pm UTC](https://www.ruby-forum.com/t/migrations-timestamp-becomes-datetime-in-mysql/60109/2 "2006-05-10T12:11:04Z")

</div>

> 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](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](http://wiki.rubyonrails.com/rails/pages/MagicFieldNames)).

Cheers Tom.

---

<div class="post-metadata">

**Author:** ![pgohman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/51bf81/32.png) [@pgohman](https://www.ruby-forum.com/u/pgohman)\
**Post date:** [May 10, 2006, 12:13pm UTC](https://www.ruby-forum.com/t/migrations-timestamp-becomes-datetime-in-mysql/60109/3 "2006-05-10T12:13:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![pgohman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/51bf81/32.png) [@pgohman](https://www.ruby-forum.com/u/pgohman)\
**Post date:** [May 10, 2006, 1:14pm UTC](https://www.ruby-forum.com/t/migrations-timestamp-becomes-datetime-in-mysql/60109/4 "2006-05-10T13:14:19Z")

</div>

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](http://api.rubyonrails.com/classes/ActiveRecord/Timestamp.html)

–

Ray
