Skip to content
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

Merged
merged 1 commit into from May 1, 2013

Conversation

chadmoone
Copy link
Contributor

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, thePostgreSQLAdapter implementation sets the default value to uuid_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 to nil, 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's before_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.)

def primary_key(name, type = :primary_key, options = {})
return super unless type == :uuid
options[:default] ||= 'uuid_generate_v4()'
Copy link
Member

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()')

@rafaelfranca
Copy link
Member

Thank you so much. Your patch is very good.

Congratulations for the nice documentation.

I did some stylistic comments.

@chadmoone
Copy link
Contributor Author

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
@chadmoone
Copy link
Contributor Author

Ok, that should include everything mentioned above. Let me know if you see anything else.

rafaelfranca added a commit that referenced this pull request May 1, 2013
Allow override of PostgreSQL UUID primary key default
@rafaelfranca rafaelfranca merged commit 09ac177 into rails:master May 1, 2013
@rafaelfranca
Copy link
Member

Thank you

@chadmoone chadmoone deleted the fix-pg-uuid-default branch May 1, 2013 23:23
rafaelfranca added a commit that referenced this pull request May 4, 2013
Allow override of PostgreSQL UUID primary key default
rafaelfranca added a commit that referenced this pull request May 4, 2013
Allow override of PostgreSQL UUID primary key default
rafaelfranca added a commit that referenced this pull request May 13, 2013
Allow override of PostgreSQL UUID primary key default
kamipo added a commit to kamipo/rails that referenced this pull request Nov 2, 2015
kamipo added a commit to kamipo/rails that referenced this pull request Jan 9, 2017
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants