Skip to content
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

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

Open
channainfo opened this issue Mar 10, 2021 · 1 comment

Comments

@channainfo
Copy link

channainfo commented Mar 10, 2021

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?

rafayet-monon added a commit to rafayet-monon/spree_related_products that referenced this issue Aug 7, 2021
add reorder(nil) for the issue - spree-contrib#191
@mcyoung
Copy link

mcyoung commented Aug 27, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants