@@ -45,7 +45,7 @@ def indexes(table_name, name = nil)
4545 def columns ( table_name , _name = nil )
4646 return [ ] if table_name . blank?
4747 column_definitions ( table_name ) . map do |ci |
48- sqlserver_options = ci . slice :ordinal_position , :is_primary , :is_identity , :default_function
48+ sqlserver_options = ci . slice :ordinal_position , :is_primary , :is_identity , :default_function , :table_name
4949 cast_type = lookup_cast_type ( ci [ :type ] )
5050 new_column ci [ :name ] , ci [ :default_value ] , cast_type , ci [ :type ] , ci [ :null ] , sqlserver_options
5151 end
@@ -192,8 +192,10 @@ def initialize_native_database_types
192192 end
193193
194194 def column_definitions ( table_name )
195- identifier = SQLServer ::Utils . extract_identifiers ( table_name )
196- database = "#{ identifier . database_quoted } ." if identifier . database_quoted
195+ identifier = SQLServer ::Utils . extract_identifiers ( table_name )
196+ database = "#{ identifier . database_quoted } ." if identifier . database_quoted
197+ view_exists = schema_cache . view_exists? ( table_name )
198+ view_tblnm = table_name_or_views_table_name ( table_name ) if view_exists
197199 sql = %{
198200 SELECT DISTINCT
199201 #{ lowercase_schema_reflection_sql ( 'columns.TABLE_NAME' ) } AS table_name,
@@ -247,6 +249,7 @@ def column_definitions(table_name)
247249 results . map do |ci |
248250 ci = ci . symbolize_keys
249251 ci [ :_type ] = ci [ :type ]
252+ ci [ :table_name ] = view_tblnm || table_name
250253 ci [ :type ] = case ci [ :type ]
251254 when /^bit|image|text|ntext|datetime$/
252255 ci [ :type ]
@@ -264,11 +267,11 @@ def column_definitions(table_name)
264267 ci [ :default_value ] ,
265268 ci [ :default_function ] = begin
266269 default = ci [ :default_value ]
267- if default . nil? && schema_cache . view_exists? ( table_name )
270+ if default . nil? && view_exists
268271 default = select_value "
269272 SELECT c.COLUMN_DEFAULT
270273 FROM #{ database } INFORMATION_SCHEMA.COLUMNS c
271- WHERE c.TABLE_NAME = '#{ table_name_or_views_table_name ( table_name ) } '
274+ WHERE c.TABLE_NAME = '#{ view_tblnm } '
272275 AND c.COLUMN_NAME = '#{ views_real_column_name ( table_name , ci [ :name ] ) } '" . squish , 'SCHEMA'
273276 end
274277 case default
0 commit comments