Skip to content

Using GUID's As Primary Keys

Ken Collins edited this page Jan 28, 2015 · 4 revisions

The latest 4.1 and 4.2 adapters have built in support for Rails UUID support.

Since this commit, we fully support GUID as primary keys. It is easy to get back GUID's for existing records as your identity columns. However, you will have to setup a few model callbacks if you want to set the identity when a record is being created. In this example below the [guid_field] is the GUID column type. We are setting it to be the #id column and when new records are created, setting the string ID.

class SqlServerEdgeSchema < ActiveRecord::Base
  
  set_primary_key :guid_field
  before_create :set_guid_column
  
  protected
  
  def set_guid_column
    self[:guid_field] ||= connection.newid_function
  end
  
end

Related Information

Clone this wiki locally