from Hacker News

Rails 4 UUID primary keys with PostgreSQL

by labria on 4/27/13, 2:43 PM with 37 comments

  • by JasonPunyon on 4/27/13, 9:40 PM

    (Caveat: I know this about SQL server, not Postgres)

    If you use random UUID's (as opposed to sequential UUID's) for your primary key your database will spend an extra hunk of time on reordering your PK index on disk on insert. This bit us at Stackoverflow. So remember: just because you can do something doesn't mean you should.

  • by tenderlove on 4/27/13, 8:25 PM

    I commented on the OP's article, but do this for the pk:

        create_table(:users, id: :uuid) do |t|
          t.string 'name'
        end
    
    And to make sure the extension is enabled, (in your migrations) do:

        enable_extension 'uuid-ossp'
    
    For now, make sure to use SQL schema dumping. I don't think Ruby schema files will work (as I haven't written a test for it yet).

    EDIT: I forgot to mention, you can enable hstore by using "enable_extension('hstore')"

  • by floormat on 4/27/13, 4:58 PM

    Sorry for the up coming stupid question but...

    What is the value of this? Why can't unique identification be done using just regular increments on an ID column? Or even a composite key?

  • by zimbatm on 4/27/13, 3:18 PM

    I think the primary_key line should be:

        t.primary_key :id, :type => :uuid
  • by jashmenn on 4/28/13, 12:15 AM

    If you'd like to use UUIDs in Rails 3, I've been maintaining the activeuuid plugin for a while now:

    https://github.com/jashmenn/activeuuid

  • by labria on 4/27/13, 10:52 PM

  • by joseph4521 on 4/27/13, 9:30 PM

    Or just use Sequel already, works with all versions of Rails (and without Rails too).
  • by flog on 4/27/13, 5:11 PM

    Now if only I could get this working with Postgres.app
  • by scotth on 4/27/13, 4:38 PM

    Can you use find_in_batches or find_each with UUIDs?