File tree Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -103,11 +103,7 @@ def visit_Arel_Nodes_SelectStatement_SQLServer_Lock collector, options = {}
103
103
end
104
104
105
105
def visit_Orders_And_Let_Fetch_Happen o , collector
106
- if ( o . limit || o . offset ) && o . orders . empty?
107
- table = table_From_Statement o
108
- column = primary_Key_From_Table ( table )
109
- o . orders = [ column . asc ]
110
- end
106
+ make_Fetch_Possible_And_Deterministic o
111
107
unless o . orders . empty?
112
108
collector << SPACE
113
109
collector << ORDER_BY
@@ -141,6 +137,17 @@ def select_statement_lock?
141
137
@select_statement && @select_statement . lock
142
138
end
143
139
140
+ def make_Fetch_Possible_And_Deterministic o
141
+ return if o . limit . nil? && o . offset . nil?
142
+ t = table_From_Statement o
143
+ pk = primary_Key_From_Table t
144
+ return unless pk
145
+ if o . orders . empty?
146
+ # Prefer deterministic vs a simple `(SELECT NULL)` expr.
147
+ o . orders = [ pk . asc ]
148
+ end
149
+ end
150
+
144
151
def table_From_Statement o
145
152
core = o . cores . first
146
153
if Arel ::Table === core . from
@@ -153,6 +160,7 @@ def table_From_Statement o
153
160
end
154
161
155
162
def primary_Key_From_Table t
163
+ return unless t
156
164
return t . primary_key if t . primary_key
157
165
if engine_pk = t . engine . primary_key
158
166
pk = t . engine . arel_table [ engine_pk ]
You can’t perform that action at this time.
0 commit comments