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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow override of PostgreSQL UUID primary key default #10404
Conversation
def primary_key(name, type = :primary_key, options = {}) | ||
return super unless type == :uuid | ||
options[:default] ||= 'uuid_generate_v4()' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
options[:default] = options.fetch(:default, 'uuid_generate_v4()')
Thank you so much. Your patch is very good. Congratulations for the nice documentation. I did some stylistic comments. |
Thanks for the feedback! My bad on the trailing spaces, I need to get my linter to check for that... I'll get the tweaks in and squashed shortly. |
without this, it's not possible to use UUID primary keys without uuid-ossp installed and activated
Ok, that should include everything mentioned above. Let me know if you see anything else. |
Allow override of PostgreSQL UUID primary key default
Thank you |
Allow override of PostgreSQL UUID primary key default
Allow override of PostgreSQL UUID primary key default
Allow override of PostgreSQL UUID primary key default
UUID primary key with no default value feature (rails#10404, rails#18206) was lost in legacy migration caused by rails#25395 got merged. Restore the feature again in legacy migration.
bc8ebef adds the ability to define tables with primary keys which use PostgreSQL's native UUID type, which is pretty sweet 馃槃.
However, because PostgreSQL does not itself provide a usable default value or UUID generator function, the
PostgreSQLAdapter
implementation sets the default value touuid_generate_v4()
, which is a function provided by an external extension (uuid-ossp
) that is not enabled by default in PostgreSQL (it is also not supported by some distributions of PostgreSQL鈥攏otably Heroku's Postgres.app).This default can currently only be overridden by either passing another UUID generation function (which will also not be enabled by default), or passing an actual static UUID value, which is not appropriate for a primary key.
This patch allows the
:default
option on a table definition to be set tonil
, which will set no default value.Setting the default value to
nil
will require you to always generate a UUID before saving a record (likely in a model's or concern'sbefore_save
callback), but it will at least allow you to use the UUID primary key feature without extensions or external libraries.I also tried to add a little documentation that will hopefully help people looking into this in the future. Please let me know if you have any feedback or suggestions.
(Tagging @tenderlove, as this is the patch the he and I discussed in the rails-core mailing list.)