The function taxonomy_select_nodes() adds a condition to field tid of table taxonomy_index. Unfortunately the table alias is not added so if you alter the query (i.e. by implementing hook_query_node_access_alter()) and add your own condition with another tid, the query horribly fails: "Integrity constraint violation: 1052 Column 'tid' in where clause is ambiguous ...".

Patch follows ...

Comments

stBorchert created an issue. See original summary.

stborchert’s picture

Status: Active » Needs review
StatusFileSize
new548 bytes

The patch simply adds the table alias for taxonomy_index.

poker10’s picture

Status: Needs review » Reviewed & tested by the community

The patch still applies to the 7.x-dev and it is working. Bug can be clearly reproduced by adding a simple join in the hook_query_node_access_alter():

function hook_query_node_access_alter(QueryAlterableInterface $query) {
  $taxonomy = FALSE;
  foreach ($query->getTables() as $alias => $table) {
    if ($table['table'] == 'taxonomy_index') {
      $taxonomy = TRUE;
    }
  }

  if ($taxonomy) {
    $query->leftJoin('taxonomy_term_data', 'ttd', 'ttd.tid = t.tid');
  }
}

You will get:

PDOException: SQLSTATE[42702]: Ambiguous column: 7 ERROR: column reference "tid" is ambiguous LINE 5: WHERE (tid = '4') ^: SELECT COUNT(t.nid) AS expression FROM {taxonomy_index} t LEFT OUTER JOIN {taxonomy_term_data} ttd ON ttd.tid = t.tid WHERE (tid = :db_condition_placeholder_0) ; Array ( [:db_condition_placeholder_0] => 4 ) in PagerDefault->execute() (line 74 of /includes/pager.inc).

Setting this as RTBC.

mcdruid’s picture

Is this a problem in D9?

Can we add #3 to the existing tests easily?

poker10’s picture

StatusFileSize
new2.63 KB
new3.17 KB

This does not seems to affect D9 - taxonomy_select_nodes() was removed and replaced by view in #2384583: Remove taxonomy_select_nodes function and related issues.

The views query in D9 looks like this (there are table aliases correctly set):

SELECT "taxonomy_index"."sticky" AS "taxonomy_index_sticky", "taxonomy_index"."created" AS "taxonomy_index_created", "node_field_data"."nid" AS "nid"
FROM
{node_field_data} "node_field_data"
LEFT JOIN {taxonomy_index} "taxonomy_index" ON node_field_data.nid = taxonomy_index.nid
WHERE ((taxonomy_index.tid = '1')) AND ("taxonomy_index"."status" = '1')
ORDER BY "taxonomy_index_sticky" DESC NULLS LAST, "taxonomy_index_created" DESC NULLS LAST
LIMIT 11 OFFSET 0

I have added the test based on that example. Patch itself is unchanged.

Note that I have to create a new taxonomy_nodes_test.module and was unable to use the existing taxonomy_test.module for the new hook, because term_view alter hooks in the taxonomy_test.module are not controlled by a variable and because of that taxonomy term detail page throws undefined propery $term->antonym when testing this scenario and displaying that page.

The last submitted patch, 5: 2574003-5_test-only.patch, failed testing. View results

mcdruid’s picture

Issue tags: +RTBM

Excellent, thanks for the D9 explanation and the tests!

  • poker10 committed 13472d3 on 7.x
    Issue #2574003 by poker10, stBorchert: Table alias missing in query...
poker10’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -RTBM

Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.