Skip to content

Commit 29a283d

Browse files
committed
Fix #rowtable_orders visitor helper to use first column if no pk column was found.
1 parent bfa3beb commit 29a283d

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
* master *
33

4+
* Fix #rowtable_orders visitor helper to use first column if no pk column was found.
5+
46
* Flatten sp_helpconstraint when looking for constraints just in case fks are present. Issue #64.
57

68
* Start to support 2011 code named "Denali".

lib/arel/visitors/sqlserver.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,10 @@ def rowtable_orders(o)
300300
core = o.cores.first
301301
if !o.orders.empty?
302302
o.orders
303-
elsif join_in_select_statement?(o)
304-
[table_from_select_statement(o).primary_key.asc]
305303
else
306-
[table_from_select_statement(o).primary_key.asc]
304+
t = table_from_select_statement(o)
305+
c = t.primary_key || t.columns.first
306+
[c.asc]
307307
end.uniq
308308
end
309309

test/cases/specific_schema_test_sqlserver.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'cases/sqlserver_helper'
22

3+
class NoPkData < ActiveRecord::Base ; self.table_name = 'no_pk_data' ; end
34
class StringDefault < ActiveRecord::Base; end;
45
class SqlServerEdgeSchema < ActiveRecord::Base; end;
56
class SqlServerEdgeSchema < ActiveRecord::Base
@@ -13,6 +14,12 @@ def set_new_id
1314

1415
class SpecificSchemaTestSqlserver < ActiveRecord::TestCase
1516

17+
should 'be able to complex count tables with no primary key' do
18+
NoPkData.delete_all
19+
10.times { |n| NoPkData.create! :name => "Test#{n}" }
20+
assert_equal 1, NoPkData.where(:name => 'Test5').count
21+
end
22+
1623
should 'quote table names properly even when they are views' do
1724
obj = SqlServerQuotedTable.create!
1825
assert_nothing_raised { SqlServerQuotedTable.first }

test/schema/sqlserver_specific_schema.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@
6767
t.column :guid, :uniqueidentifier
6868
end
6969

70+
create_table :no_pk_data, :force => true, :id => false do |t|
71+
t.string :name
72+
end
73+
7074
execute %|ALTER TABLE [sql_server_edge_schemas] ADD [guid_newid] uniqueidentifier DEFAULT NEWID();|
7175
execute %|ALTER TABLE [sql_server_edge_schemas] ADD [guid_newseqid] uniqueidentifier DEFAULT NEWSEQUENTIALID();|
7276

0 commit comments

Comments
 (0)