On Tue, Aug 7, 2012 at 5:53 AM, Ken Collins <
k...@metaskills.net> wrote:
> 2) It is really easy to setup a schema where the database champions
> assigning a new unique identifier. However, getting that key back on
> create/insert is a bit tricky since most low level connection modes do not
> treat unique identifier columns as real primary keys. Hence if you let the
> db generate the value, an insert would have to be followed by another
> crafted select. The SQL Server supers up to ActiveRecord's #last_inserted_id
> and falls back to SCOPE_IDENTITY() which is fine for normal primary keys,
> but I do not know of a way to do the same (even assuming I reflected on the
> table having unique identifier or not) to get the random string that is a
> uuid column. A show stopper in itself.
ActiveRecord should be able to take the same approach that Sequel
uses, returning all of the inserted column values when inserting using
OUTPUT:
DB.create_table(:ts) do
String :id, :primary_key=>true, :default=>Sequel.function(:newid)
String :name
end
class T < Sequel::Model; end
T.create(:name=>'foo')
# SQL: INSERT INTO [TS] ([NAME]) OUTPUT [INSERTED].* VALUES (N'foo')
# => #<T @values={:id=>"41BE3F26-8DE4-46FA-9A92-582CA33835B2", :name=>"foo"}>
Jeremy