New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SQL Server: CPK 8.1.0 and activerecord-sqlserver-adapter 4.2.4 generate wrong SQL in some queries on tables without a DB primary key #316
Comments
Look at the connection_adapters folder - that's where we put overrides to ActiveRecord connection adapters. So could the adapter be fixed instead? |
In a project with composite_primary_keys (cpk), we experienced a similar error. It was noticeable when calling MODEL.reload. It would incorrectly form primary keys in order by, like [MODEL][pk1, pk2]. In order to make it work, we used code suggested by @bunyan from above, but we patched the gem method make_Fetch_Possible_And_Deterministic directly. (the difference is that it gets run only when o.orders are empty and this worked for us) def make_Fetch_Possible_And_Deterministic o
return if o.limit.nil? && o.offset.nil?
t = table_From_Statement o
pk = primary_Key_From_Table t
return unless pk
if o.orders.empty?
# Prefer deterministic vs a simple `(SELECT NULL)` expr.
o.orders = [pk.asc]
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 We found that the first time the query was loaded the cpks were rendered in a way that worked for the query: |
Has anyone been able to fix or come up with a more straight forward solution than monkey patching things? I'm also running into this same issue:
|
For what its worth, the latest release has the best sql servers support CPK has ever had. But it does require Active Record 6. For old CPK releases that are no longer supported, yes, just make a monkey patch. Feel free to submit a patch and it can be integrated in if tests pass. |
Can you show the full stack trace? |
If I have a table with no primary key in DB defined like this:
and a corresponding model with CPK
Calling
will result in the following error:
I actually faced this with a belongs_to association but this is a simpler case.
In make_Fetch_Possible_And_Deterministic (/activerecord-sqlserver-adapter-4.2.4/lib/arel/visitors/sqlserver.rb:142), the adapter gets the composite key for a table and applies the sorting order to the whole "array" key which then results in wrong SQL. It's hardly a bug of the sqlserver-adapter so I'm posting it here.
To make things work for me I overrode the method in a Rails initializer to expand the query:
Not sure how to incorporate this into the gem though.
A gist that reproduces the bug: https://gist.github.com/bunyan/1ea31f8b6ce8290c50b6
The text was updated successfully, but these errors were encountered: