File tree Expand file tree Collapse file tree 1 file changed +11
-7
lines changed
lib/active_record/connection_adapters/sqlserver Expand file tree Collapse file tree 1 file changed +11
-7
lines changed Original file line number Diff line number Diff line change @@ -371,6 +371,15 @@ def column_definitions(table_name)
371371 view_exists = view_exists? ( table_name )
372372 view_tblnm = view_table_name ( table_name ) if view_exists
373373
374+ if view_exists
375+ results = sp_executesql %{
376+ SELECT c.COLUMN_NAME AS [name], c.COLUMN_DEFAULT AS [default]
377+ FROM #{ database } .INFORMATION_SCHEMA.COLUMNS c
378+ WHERE c.TABLE_NAME = #{ quote ( view_tblnm ) }
379+ } . squish , "SCHEMA" , [ ]
380+ default_functions = results . each . with_object ( { } ) { |row , out | out [ row [ "name" ] ] = row [ "default" ] } . compact
381+ end
382+
374383 sql = column_definitions_sql ( database , identifier )
375384
376385 binds = [ ]
@@ -402,13 +411,8 @@ def column_definitions(table_name)
402411 ci [ :default_function ] = begin
403412 default = ci [ :default_value ]
404413 if default . nil? && view_exists
405- default = select_value %{
406- SELECT c.COLUMN_DEFAULT
407- FROM #{ database } .INFORMATION_SCHEMA.COLUMNS c
408- WHERE
409- c.TABLE_NAME = '#{ view_tblnm } '
410- AND c.COLUMN_NAME = '#{ views_real_column_name ( table_name , ci [ :name ] ) } '
411- } . squish , "SCHEMA"
414+ view_column = views_real_column_name ( table_name , ci [ :name ] )
415+ default = default_functions [ view_column ] if view_column . present?
412416 end
413417 case default
414418 when nil
You can’t perform that action at this time.
0 commit comments