spree-contrib / spree_related_products

Related products extension for Spree Commerce.
http://guides.spreecommerce.org
BSD 3-Clause "New" or "Revised" License
149 stars 241 forks source link

product_relation_types causes error in Postgresql with the order sql statement #191

Open channainfo opened 3 years ago

channainfo commented 3 years ago

product_relation_types method in the following view

<% if product_relation_types.any? %>
  <div id="related-products">
    <% product_relation_types.each do |relation_type| %>
      <% products = product_relations_by_type(relation_type) %>

      <% if products.any? %>
        <div class="product-details-related" id="product-details-related-<%= relation_type.id %>">
          <h2 class="font-weight-bold text-center text-uppercase product-details-carousel-text">
            <%= relation_type.display_name %>
          </h2>
          <%#= render 'spree/shared/carousel_4_products', id: "related-products-carousel-#{relation_type.id}", products: products %>
          <%= render 'vshop/shared/carousel_for_products', id: 'related-products-carousel', products: products %>

        </div>
      <% end %>
    <% end %>
  </div>
<% end %>

cause the following errors

ActiveRecord::StatementInvalid (PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list) LINE 1: ...ble_type" = $2 / loading for inspect / ORDER BY "spree_rel

SQL generated:

Spree::Product.last.relation_types.to_sql

SELECT DISTINCT "spree_relation_types".* 
FROM "spree_relation_types" 
INNER JOIN "spree_relations" ON "spree_relation_types"."id" = "spree_relations"."relation_type_id" 
WHERE "spree_relations"."relatable_id" = 14 AND "spree_relations"."relatable_type" = 'Spree::Product' 
ORDER BY "spree_relations"."position" ASC

The similar error has been raised here:

https://stackoverflow.com/questions/9795660/postgresql-distinct-on-with-different-order-by.

Is there anyone having this issue?

Tested with the following env:

I tried to fix this with by resetting the order by clause:

 base.has_many :relation_types, -> { distinct.reorder(nil) }, class_name: 'Spree::RelationType', through: :relations

Any comments on this?

mcyoung commented 2 years ago

See the same issue w/ a MySQL db, and same fix appears to make things happy