{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/14300","labels_url":"https://api.github.com/repos/rails/rails/issues/14300/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/14300/comments","events_url":"https://api.github.com/repos/rails/rails/issues/14300/events","html_url":"https://github.com/rails/rails/issues/14300","id":28893904,"number":14300,"title":"Support dynamic postgres column defaults ","user":{"login":"searls","id":79303,"avatar_url":"https://avatars.githubusercontent.com/u/79303?v=3","gravatar_id":"","url":"https://api.github.com/users/searls","html_url":"https://github.com/searls","followers_url":"https://api.github.com/users/searls/followers","following_url":"https://api.github.com/users/searls/following{/other_user}","gists_url":"https://api.github.com/users/searls/gists{/gist_id}","starred_url":"https://api.github.com/users/searls/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/searls/subscriptions","organizations_url":"https://api.github.com/users/searls/orgs","repos_url":"https://api.github.com/users/searls/repos","events_url":"https://api.github.com/users/searls/events{/privacy}","received_events_url":"https://api.github.com/users/searls/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/rails/labels/pinned","name":"pinned","color":"f7c6c7"},{"url":"https://api.github.com/repos/rails/rails/labels/PostgreSQL","name":"PostgreSQL","color":"fbca04"}],"state":"open","locked":false,"assignee":{"login":"tenderlove","id":3124,"avatar_url":"https://avatars.githubusercontent.com/u/3124?v=3","gravatar_id":"","url":"https://api.github.com/users/tenderlove","html_url":"https://github.com/tenderlove","followers_url":"https://api.github.com/users/tenderlove/followers","following_url":"https://api.github.com/users/tenderlove/following{/other_user}","gists_url":"https://api.github.com/users/tenderlove/gists{/gist_id}","starred_url":"https://api.github.com/users/tenderlove/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tenderlove/subscriptions","organizations_url":"https://api.github.com/users/tenderlove/orgs","repos_url":"https://api.github.com/users/tenderlove/repos","events_url":"https://api.github.com/users/tenderlove/events{/privacy}","received_events_url":"https://api.github.com/users/tenderlove/received_events","type":"User","site_admin":false},"milestone":null,"comments":10,"created_at":"2014-03-06T16:42:46Z","updated_at":"2015-06-27T10:36:58Z","closed_at":null,"body":"I was surprised to find that I couldn't write the following migration when using postgresql:\r\n\r\n``` ruby\r\nclass CreateMessages < ActiveRecord::Migration\r\n  def change\r\n    create_table :messages do |t|\r\n      t.timestamp :received_at, default: \"now()\"\r\n    end\r\n  end\r\nend\r\n```\r\n\r\nDoing so will immediately evaluate `now()` such that a psql description of the table will report the default is something like `default '2014-03-06 16:16:45.108266'::timestamp without time zone`.\r\n\r\nI worked around this in my migration by using an alter statement immediately after creating the table:\r\n\r\n``` ruby\r\nActiveRecord::Base.connection.execute \"ALTER TABLE messages ALTER COLUMN received_at SET DEFAULT now()\"\r\n```\r\n\r\nThis seemed to solve our issue (the description of the column default was `default now()` and it worked fine).\r\n\r\nHowever, any rake command dependent on `db:schema:load` and the schema.rb file are causing exactly the same issue, because `db:schema:dump` records this into my `db/schema.rb` file:\r\n\r\n``` ruby\r\nt.datetime \"received_at\", default: \"now()\"\r\n```\r\n\r\nBecause `db/schema.rb` is generated and checked into source control, I'm struggling to think of any graceful workarounds to the issue. "},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/116002622","html_url":"https://github.com/rails/rails/issues/14300#issuecomment-116002622","issue_url":"https://api.github.com/repos/rails/rails/issues/14300","id":116002622,"user":{"login":"tejasbubane","id":980783,"avatar_url":"https://avatars.githubusercontent.com/u/980783?v=3","gravatar_id":"","url":"https://api.github.com/users/tejasbubane","html_url":"https://github.com/tejasbubane","followers_url":"https://api.github.com/users/tejasbubane/followers","following_url":"https://api.github.com/users/tejasbubane/following{/other_user}","gists_url":"https://api.github.com/users/tejasbubane/gists{/gist_id}","starred_url":"https://api.github.com/users/tejasbubane/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tejasbubane/subscriptions","organizations_url":"https://api.github.com/users/tejasbubane/orgs","repos_url":"https://api.github.com/users/tejasbubane/repos","events_url":"https://api.github.com/users/tejasbubane/events{/privacy}","received_events_url":"https://api.github.com/users/tejasbubane/received_events","type":"User","site_admin":false},"created_at":"2015-06-27T10:36:58Z","updated_at":"2015-06-27T10:36:58Z","body":"@searls Yes, the [Column Modifiers section on rails-guides](https://github.com/rails/rails/blob/master/guides/source/active_record_migrations.md#column-modifiers) states that when using dynamic values like date, the default will only be calculated for the first time.\r\n\r\n@davekapp Thanks for the workaround."}}