When a product has 2 or more attributes, this is the error in the adjustment tab:
(http://www.mysite.com/product1#overlay-context=product1&overlay=node/1/e...)

PDOException: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "WHERE" LINE 9: WHERE (ao1.aid = '3') AND (ao2.aid = '1') ) subquery ^: SELECT COUNT(*) AS expression FROM (SELECT 1 AS expression FROM {uc_product_options} po1 LEFT OUTER JOIN {uc_attribute_options} ao1 ON po1.oid = ao1.oid AND po1.nid = :nid INNER JOIN {uc_product_options} po2 LEFT OUTER JOIN {uc_attribute_options} ao2 ON po2.oid = ao2.oid AND po2.nid = :nid WHERE (ao1.aid = :db_condition_placeholder_0) AND (ao2.aid = :db_condition_placeholder_1) ) subquery; Array ( [:db_condition_placeholder_0] => 3 [:db_condition_placeholder_1] => 1 [:nid] => 1 ) in PagerDefault->execute() (line 74 of /srv/http/mysite.com/http/www/includes/pager.inc).

Thanks
f.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TR’s picture

Status: Active » Postponed (maintainer needs more info)

I can't reproduce this. How many options do each of your attributes have?

francesco.g’s picture

Hi TR,
with 1 product and 1 attribute (2 options for this attribute) when click on the adjustment tab this is the error:

PDOException: SQLSTATE[42803]: Grouping error: 7 ERROR: column "a.aid" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: ...isplay, a.name AS name, a.ordering AS a_ordering, a.aid AS a... ^: SELECT pa.nid AS nid, pa.aid AS aid, pa.ordering AS ordering, pa.display AS display, a.name AS name, a.ordering AS a_ordering, a.aid AS a_aid, ao.aid AS ao_aid FROM {uc_product_attributes} pa LEFT OUTER JOIN {uc_attributes} a ON pa.aid = a.aid LEFT OUTER JOIN {uc_attribute_options} ao ON a.aid = ao.aid LEFT OUTER JOIN {uc_product_options} po ON ao.oid = po.oid AND po.nid = :po_nid WHERE (pa.nid = :db_condition_placeholder_0) GROUP BY ao.aid, pa.aid, pa.display, a.name, pa.ordering, a.ordering, pa.nid HAVING (COUNT(po.oid) > 0) ; Array ( [:db_condition_placeholder_0] => 1 [:po_nid] => 1 ) in uc_product_adjustments_form() (line 1222 of /srv/mysite/http/www/sites/all/modules/ubercart/uc_attribute/uc_attribute.admin.inc).

In srv/mysite/http/www/sites/all/modules/ubercart/uc_attribute/uc_attribute.admin.inc - line 1220, if you add
->groupBy('a.aid')
adjustment tab works fine; you can manage the Alternate SKU.
With this change (->groupBy('a.aid')) adjustment tab works only with one attribute.

But if you add a second attribute in http://www.mysite.it/node/1#overlay=admin/store/products/attributes/add
and then you add an option (one or more) in http://www.mysite.it/node/1#overlay=admin/store/products/attributes/2/op...

and then, in http://www.mysite.it/node/1#overlay=node/1/edit/attributes You add the second attribute to the product

when click on the adjustment tab this is the error

PDOException: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "WHERE" LINE 9: WHERE (ao1.aid = '1') AND (ao2.aid = '2') ) subquery ^: SELECT COUNT(*) AS expression FROM (SELECT 1 AS expression FROM {uc_product_options} po1 LEFT OUTER JOIN {uc_attribute_options} ao1 ON po1.oid = ao1.oid AND po1.nid = :nid INNER JOIN {uc_product_options} po2 LEFT OUTER JOIN {uc_attribute_options} ao2 ON po2.oid = ao2.oid AND po2.nid = :nid WHERE (ao1.aid = :db_condition_placeholder_0) AND (ao2.aid = :db_condition_placeholder_1) ) subquery; Array ( [:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => 2 [:nid] => 1 ) in PagerDefault->execute() (line 74 of /srv/mysite/http/www/includes/pager.inc).

This with Drupal 7.22, Ubercart 7.x-3.4, (or Ubercart 7.x-3.x dev) Rules 7.x -2.3., Views 7.x -3.7., ctools 7.x -1.3., Entity 7.x -1.1.
Thanks
f.

longwave’s picture

Which database server are you using? MySQL, Postgres, something else?

francesco.g’s picture

Hi lonwave,
Postgresql

TR’s picture

Issue tags: +PostgreSQL

Drupal has a database abstraction layer that is supposed to insulate modules like Ubercart from the differences between various databases. The SQL query that's causing the problem isn't actually constructed by Ubercart, it's formed by the database abstraction layer given the parameters that Ubercart specifies. If there's a grouping error (your first error) or a syntax error (your second error), that indicates the database abstraction layer didn't do a good job of constructing the query for PostgreSQL. Likewise, almost any error of this type where a query fails on PostgreSQL but works on MySQL indicates a problem with the Drupal database API implementation, not with the module (Ubercart) doing the query. Therefore, you should first check the Drupal core issue queue for any open (or closed) issues similar to what you're reporting and see if there is a proposed solution. If not, you should isolate the exact SQL that is being generated by the database abstraction layer, as well as the module code (e.g. Ubercart code) that is being used, and submit that as a bug report in the core Drupal issue queue.

In the future, realize that as a PostgreSQL user, you are in a small minority. ANY database error you see is potentially, even likely, due to Drupal's PostgreSQL support. Because of this, the FIRST thing you should mention when submitting a bug report about a database error is that you're using PostgreSQL.

longwave’s picture

Title: product attribute PDOException » Product attribute adjustment page has PDOException with PostgreSQL
Status: Postponed (maintainer needs more info) » Active

Better title, no more info required, now we need someone with Postgres to debug this and determine if we should fix it in Ubercart or Drupal core.

TR’s picture

In this case, it's almost certainly a core issue, but we may be able to avoid it by rewriting the query (which is something that we shouldn't have to do, as it will make for fragile and seemingly arbitrary code).

One of PostgreSQL's quirks is that if you group the query results, you must specify every column of the result set in your GROUP BY. However, the Drupal database API doesn't require a groupBy() for every column - it's the job of the DB abstraction layer to fill in all the non-specified column names when it generates the SQL for PostgreSQL. The original poster already tried the first-line fix of specifying groupBy('a.aid') explicitly to circumvent the failure of the DB abstraction layer in this instance, which fixed the initial problem but lead to a syntax error later down the road. This is where it starts to get touchy - we're trying to tweak the generated SQL by making small, non-functional changes at the API level, in effect trying to guess how to force the SQL to be what we want by what I term "asking the same question" in a different way. I'm sure this can be done at the Ubercart level if we had someone with PostgreSQL willing to work on it, but I'm also sure this is the wrong place to fix it in the long run.

petrjanda’s picture

Has anyone been able to fix this problem? I'm also using PostgreSQL and get this error.

Island Usurper’s picture

Wow. Postgres requires ON conditions for INNER JOIN, even when you don't want to reduce the number of results. Postgres provides CROSS JOIN to make that easier, but I don't think Drupal's DB API does. But we can probably say

$query->join('uc_product_options', "po$i", '(TRUE)');

The Postgres documentation mentions this, and I'll test it myself since I'm using Postgres.

PeterSprague’s picture

Any resolution or work-around for this?

I need attributes to offer trial discounts to webapps. Using drupal as the front-end for subscriptions.

Peter

longwave’s picture

Title: Product attribute adjustment page has PDOException with PostgreSQL » Product attribute adjustment page has PDOException with PostgreSQL and SQL Server

Noted in #2044223: SQL Server Compatiblity that SQL Server has the same issue with this query.

vitalijus.trainys@gmail.com’s picture

Patch for PostgreSQL is simple: add line #1221, ->groupBy('a.aid') in function uc_product_adjustments_form, in file uc_attribute.admin.inc.

TR’s picture

Issue summary: View changes
Priority: Normal » Minor
Status: Active » Needs work

vitalijus.trainys@gmail.com: According to #2, adding ->groupBy('a.aid') will cause a syntax error.

If you would like to help out on this issue, what we need is a PATCH. A patch will allow the automated tests to check the proposed modification, will allow other PostgreSQL and SQL Server users to test your fix, and will allow MySql users to make sure the proposed modification doesn't break existing functionality.

Ubercart 8.x-4.x works properly with PostgreSQL.

david_garcia’s picture

Status: Needs work » Needs review
FileSize
463 bytes

Both POSGRE and SQLSRV require that all columns in the select list are part of the aggregate.

MYSQL but it's known for it's lack of formal correctness (which means bread for today and hunger for tomorrow). It is basically too loose in many aspects.

This is WHY all columns should be present in the aggregate:

http://weblogs.sqlteam.com/jeffs/archive/2007/07/20/but-why-must-that-co...

Posgre, MSSQL and Oracle are real database engines, and thus they enforce such proper usage patterns.

The Drupal database abstraction layer does not make any assumptions about this requirement. But.... making the the database drivers expand missing columns into the aggregate will be a formal mistake. It's like forcefully introducing a MySQL bug just to make them behave like MySQL.

As for the concerns in #2 of this change breaking something in posgre, note that the error is a SYNTAX error. That clearly points to something wrong with the query builder, or to a NON RELATED issue.

I'm patching with the original proposal.

david_garcia’s picture

I resqued another fix from a related issue that is of the same nature: adding a field to an aggregate query and the field not being part of the aggregate.

TR’s picture

In #2 the original poster said that adding ->groupBy('a.aid') works to fix the grouping for the PostgreSQL query, but the functionality of the page isn't fixed because there is an additional PostgreSQL problem when you have multiple attributes.

In #9 Island Usurper pointed out the possible reason for the syntax error with multiple attributes, but he never followed up with a patch. Nor did any other PostgreSQL user.

Did you test the multiple attribute use case described in #2? I would like to fix *all* the PostgreSQL problems on the product adjustment page which have been mentioned here.

It doesn't affect how we fix the problem, but my point about the abstraction layer is that it should *never* generate syntactically incorrect SQL, and that the whole purpose of the abstraction layer is to hide the different DB SQL quirks from the API user. So DBTNG should handle the GROUP BY differences either by raising an error (instead of generating and sending incorrect SQL to the DB) OR by expanding the GROUP BY clause so as to effect the same results regardless of DB (I suppose this means expanding so that the query emulates MySQL when not all columns are specified in the GROUP BY). If DBTNG code works properly with one DB but not with another, that means DBTNG is broken, as that is its raison d'etre.

TR’s picture

Status: Needs review » Needs work