Last active
October 14, 2020 13:54
-
-
Save bunyan/1ea31f8b6ce8290c50b6 to your computer and use it in GitHub Desktop.
Rails 4.2: composite_primary_keys 8.1.0 and activerecord-sqlserver-adapter 4.2.4 generating wrong SQL on some queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateNoIdModels < ActiveRecord::Migration | |
def change | |
create_table :no_id_models, id: false do |t| | |
t.integer :pk1 | |
t.integer :pk2 | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'rails', '4.2.3' | |
gem 'composite_primary_keys', '8.1.0' | |
gem 'activerecord-sqlserver-adapter', '4.2.4' | |
gem 'tiny_tds', '0.6.2' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NoIdModel < ActiveRecord::Base | |
self.primary_keys = [:pk1, :pk2] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class NoIdModelTest < ActiveSupport::TestCase | |
test 'model with no id limited by 1' do | |
assert NoIdModel.limit(1).to_a.is_a?(Array) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NoIdModel.create([{ pk1: 1, pk2: 2 }, { pk1: 3, pk2: 4 }]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# put this in the initializers folder | |
# requires Ruby 2+ | |
module SQLServerCPKOrderFix | |
def make_Fetch_Possible_And_Deterministic o | |
super | |
o.orders.each_with_index do |node, i| | |
rel = node.expr.relation | |
expr_name = node.expr.name | |
if expr_name.is_a?(CompositePrimaryKeys::CompositeKeys) | |
o.orders[i] = expr_name.collect { |a| rel[a].send(node.direction) } | |
end | |
end | |
end | |
end | |
Arel::Visitors::SQLServer.prepend(SQLServerCPKOrderFix) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this solution. Tested with
activerecord 6.0.3.4
👍Had to update line 10 as @hechien suggested.