-
Notifications
You must be signed in to change notification settings - Fork 21.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support dynamic postgres column defaults #14300
Comments
Would using the schema dump in .sql rather than .rb help? We had to use that to get support for some other PostgreSQL stuff in the past. I'm not sure if this will fix your issue but I figured it was worth asking. :) |
Wow @davekapp, fantastic! That totally did the trick. Great workaround idea. For anyone finding this issue via Google, I worked around the issue by setting in my #...
class Application < Rails::Application
config.active_record.schema_format = :sql
end
#... And running:
I verified it worked by checking that my test database's default for the column was set to |
Woo hoo, glad it helped! 👍 Friendly warning that .sql schema dumps can scare Heroku. It's not too hard to get around though, here's what we did when I needed to take care of it: In application.rb:
|
Hello there, Great work here! What do you think about mentioning this is the Have a nice day. |
Hi Robin, Thanks, I appreciate your kind words! :) Things are really hectic right during the first part of this week due to a release we're tying to wrap up but I'll work on a pull request shortly thereafter, likely Wednesday. Could I contact you for feedback on the PR formatting (or should I just do the first pass at it and then ask for feedback afterwards)? |
Generally you should open the pull request and then people discuss about the change. To be honest I'm not a merger so the decision is not up to me. |
This issue has been automatically marked as stale because it has not been commented on for at least The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the Thank you for all your contributions. |
This issue has been automatically marked as stale because it has not been commented on for at least The resources of the Rails team are limited, and so we are asking for your help. If you can still reproduce this error on the Thank you for all your contributions. |
I can still reproduce this on 4.2. |
@searls Yes, the Column Modifiers section on rails-guides states that when using dynamic values like date, the default will only be calculated for the first time. @davekapp Thanks for the workaround. |
Just to update people who were searching for an answer. As for > Rails 4.2, defining the default with proc works: It produces the expected |
I was surprised to find that I couldn't write the following migration when using postgresql:
Doing so will immediately evaluate
now()
such that a psql description of the table will report the default is something likedefault '2014-03-06 16:16:45.108266'::timestamp without time zone
.I worked around this in my migration by using an alter statement immediately after creating the table:
This seemed to solve our issue (the description of the column default was
default now()
and it worked fine).However, any rake command dependent on
db:schema:load
and the schema.rb file are causing exactly the same issue, becausedb:schema:dump
records this into mydb/schema.rb
file:Because
db/schema.rb
is generated and checked into source control, I'm struggling to think of any graceful workarounds to the issue.The text was updated successfully, but these errors were encountered: