spree_related_products icon indicating copy to clipboard operation
spree_related_products copied to clipboard

product_relation_types causes error in Postgresql with the order sql statement

Open channainfo opened this issue 3 years ago • 1 comments

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:

  • spree 4.2.0
  • spree_related_products 3.4.0

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?

channainfo avatar Mar 10 '21 06:03 channainfo

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

mcyoung avatar Aug 27 '22 20:08 mcyoung