Forum: Ruby on Rails help:How to set default value for a column use of migration?

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.
A8ec3c62a03f85e178f33e590e63da6f?d=identicon&s=25 Jay (Guest)
on 2007-04-15 08:09
(Received via mailing list)
hi,all..
Here i want to use of migration to create a table and set a defult
value for a column..but i cant't finished..

mycode:
class CreateMessages < ActiveRecord::Migration
  def self.up
    create_table :messages do |t|
      t.column :name,     :string
      t.column :content,  :text
      t.column :posttime, :datetime, :default => "now()"
    end
  end

  def self.down
    drop_table :messages
  end
end

it can't work sucessfully.And the error like this:
(in G:/Jc_RubyonRails/InstantRails/rails_apps/guestbook)
== CreateMessages: migrating
==================================================
-- create_table(:messages)
rake aborted!
Mysql::Error: Invalid default value for 'posttime': CREATE TABLE
messages (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `name`
varchar(255) DEFAULT NULL, `content` text DEFAULT NULL, `posttime`
datetime DEFAULT 'now()') ENGINE=InnoDB

(See full trace by running task with --trace)

someone could help me.thanks.
40db9e75b3f5899258e3bdc0c9210154?d=identicon&s=25 Conrad Taylor (Guest)
on 2007-04-16 02:54
(Received via mailing list)
Hi, you can do one of the following:

t.column :posttime, :datetime, :default => Time.now

or

t.column :created_at, :datetime

Good luck,

-Conrad
This topic is locked and can not be replied to.