Jeremy, That is just beautiful! In your example, that is just a string column with a default, not a real [uniqueidentifier] data type. But I suspect that is moot since this should work with any datatype.
So the adapter could support something like that. If ActiveRecord did move forward with it, we might need something like @uuid ivar/accessor to the Column object... perhaps not tho. Maybe the SQL Server adapter could just use the OUTPUT method pointed to the primary key column no matter what and it should all work now and in the future. I may look into that one day. - Ken On Aug 7, 2012, at 11:33 AM, Jeremy Evans wrote: > On Tue, Aug 7, 2012 at 5:53 AM, Ken Collins <[email protected]> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
