D7.12
forum.module around l. 779 raises a PDO Exception.

There seem to be two issues related to the sql inserted by
$query ...
->addTag('node_access') on l.778

First this makes the reference to status ambiguous in l. 776
I changed that to
->condition('n.status', 1)
Which helped but then I got the error below
(which goes away if the addTag('node_access') line is removed).
I couldn't figure out how to clean that up.

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'node.tid' in 'on clause': SELECT f.tid AS tid, COUNT(n.nid) AS topic_count, SUM(ncs.comment_count) AS comment_count FROM {node} n INNER JOIN {node_comment_statistics} ncs ON n.nid = ncs.nid INNER JOIN {forum} f ON n.vid = f.vid INNER JOIN {node} node ON f.tid = node.tid WHERE (n.status = :db_condition_placeholder_0) AND (n.language IN (:db_condition_placeholder_1, :db_condition_placeholder_2)) GROUP BY tid; Array ( [:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => en [:db_condition_placeholder_2] => und ) in forum_forum_load() (line 779 of /home/dharmaga/public_html/dev.dharmagarasia.info/modules/forum/forum.module).

Comments

fergusong’s picture

Title: PDO Exception probably related to addTag('node_access') » forum.module D7.12 PDO Exception probably related to addTag('node_access')
larowlan’s picture

What node access modules do you have enabled?

larowlan’s picture

Status: Active » Postponed (maintainer needs more info)
larowlan’s picture

Version: 7.9 » 8.x-dev
Status: Postponed (maintainer needs more info) » Needs review
Issue tags: +Needs backport to D7
StatusFileSize
new542 bytes

Status: Needs review » Needs work

The last submitted patch, 1430386-unknown-tid.patch, failed testing.

larowlan’s picture

Status: Needs work » Needs review
StatusFileSize
new466 bytes
fergusong’s picture

Thanks for the response,

The access control modules I have enabled are ACL and Content Access.
I also have most of the i18n modules enabled.

I updated my forum.module file per the patch, but it didn't solve the problem.
If I comment out the addTag('node_access') line it works fine either way.
The code I've got from l. l775
if (count($_forums)) {
$query = db_select('node', 'n');
$query->join('node_comment_statistics', 'ncs', 'n.nid = ncs.nid');
$query->join('forum', 'f', 'n.vid = f.vid');
$query->addExpression('COUNT(n.nid)', 'topic_count');
$query->addExpression('SUM(ncs.comment_count)', 'comment_count');
$counts = $query
->fields('f', array('tid'))
->condition('n.status', 1) // gjf added n. prefix
->groupBy('f.tid') // gjf: added f. prefix
->addTag('node_access') // gjf: wondering
->execute()
->fetchAllAssoc('tid');
}

The report
PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'node.tid' in 'on clause': SELECT f.tid AS tid, COUNT(n.nid) AS topic_count, SUM(ncs.comment_count) AS comment_count FROM {node} n INNER JOIN {node_comment_statistics} ncs ON n.nid = ncs.nid INNER JOIN {forum} f ON n.vid = f.vid INNER JOIN {node} node ON f.tid = node.tid WHERE (n.status = :db_condition_placeholder_0) AND (n.language IN (:db_condition_placeholder_1, :db_condition_placeholder_2)) GROUP BY f.tid; Array ( [:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => en [:db_condition_placeholder_2] => und ) in forum_forum_load() (line 779 of /home/dharmaga/public_html/dev.dharmagarasia.info/modules/forum/forum.module).

oriol_e9g’s picture

Status: Needs work » Reviewed & tested by the community

Edited by mistake in the comment.

oriol_e9g’s picture

Status: Needs review » Needs work
larowlan’s picture

Status: Reviewed & tested by the community » Postponed (maintainer needs more info)

The add tag node access tells your node access modules to rewrite the query and invoke their access limitations. Without the add tag, you won't get expected access controls. I suspect the issue is with the rewriting as the join on tid isn't done by core. Please disable acl and content access and report back.

trigdog’s picture

I am also having this problem. I have similar modules enabled (Content Access, ACL, i18n) on D7.12 (Didn't have this issues on D7.10). Will try disabling different modules and report back. Saw this over in the Internationalization issues and thought they might be related - http://drupal.org/node/1429962

fergusong’s picture

I disabled those modules but the problem continued.

The problematic sql seems to be
INNER JOIN {node} node ON f.tid = node.tid
and that sees to be created in the 'node_access' processing...

The problem occurs for my admin account but not for other roles.
More specifically, looking at _node_query_node_access_alter() in node.module
If I get to l.3170 (i.e. don't have 'bypass node access') then the
bug occurs. That seems odd, why is it doing anything
if it doesn't get there? But it is what I'm seeing.

larowlan’s picture

see also #1431452: Tests for 'status' in where clause is ambiguous for the fix to the status issue

larowlan’s picture

can you add this to the end of node_query_node_access_alter function

drupal_set_message((string)$query);

and paste the output here

fergusong’s picture

In a failure case I see
SELECT f.tid AS tid, COUNT(n.nid) AS topic_count, SUM(ncs.comment_count) AS comment_count FROM {node} n INNER JOIN {node_comment_statistics} ncs ON n.nid = ncs.nid INNER JOIN {forum} f ON n.vid = f.vid INNER JOIN {node} node ON f.tid = node.tid WHERE (n.status = :db_condition_placeholder_0) AND (n.language IN (:db_condition_placeholder_1, :db_condition_placeholder_2)) GROUP BY tid

Also: I can't replicate the odd result with varying Roles, I must have
screwed up the test cases somewhere. Apologies for that false track.

superthin’s picture

Drupal 7.12 when visiting my site http://mysite.com/forum

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'status' in where clause is ambiguous: SELECT f.tid AS tid, COUNT(n.nid) AS topic_count, SUM(ncs.comment_count) AS comment_count FROM {node} n INNER JOIN {node_comment_statistics} ncs ON n.nid = ncs.nid INNER JOIN {forum} f ON n.vid = f.vid INNER JOIN {node} node ON f.tid = node.tid WHERE (status = :db_condition_placeholder_0) AND (n.language IN (:db_condition_placeholder_1, :db_condition_placeholder_2)) GROUP BY tid; Array ( [:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => vi [:db_condition_placeholder_2] => und ) trong advanced_forum_forum_load() (dòng 142 của /var/www/clients/client1/web29/web/sites/all/modules/advanced_forum/includes/core-overrides.inc).

sbasurto’s picture

Status: Fixed » Postponed (maintainer needs more info)

DONT USE THIS see #18
I just did it for testing prouposes, this is not a solution to the problem also see #23

I receive the same error

PDOException: SQLSTATE[42703]: Undefined column: 7 ERROR: column node.tid does not exist LINE 6: INNER JOIN node node ON f.tid = node.tid ^: SELECT f.tid AS tid, COUNT(n.nid) AS topic_count, SUM(ncs.comment_count) AS comment_count FROM {node} n INNER JOIN {node_comment_statistics} ncs ON n.nid = ncs.nid INNER JOIN {forum} f ON n.vid = f.vid INNER JOIN {node} node ON f.tid = node.tid WHERE (node.status = :db_condition_placeholder_0) AND (n.language IN (:db_condition_placeholder_1, :db_condition_placeholder_2)) GROUP BY f.tid; Array ( [:db_condition_placeholder_0] => 1 [:db_condition_placeholder_1] => es [:db_condition_placeholder_2] => und ) en forum_forum_load() (line 779 of modules/forum/forum.module).

and surpas it doing the following:

alter table node rename column tnid to tid;

and in the file forum.module change the lines:

776 ->condition('status', 1)
776 ->condition('node.status', 1) // This is the new one
777 ->groupBy('tid')
777 ->groupBy('f.tid') // This is the new one

I am sure this is not the right way, because it "fix" forum but breaks all the site, the problem is that the JOIN is being doing with "tid" in the "forum module", but in "node" table this column does not exist.

Regards,

bak’s picture

Version: 8.x-dev » 7.12

DON'T DO THE "FIX":
alter table node rename column tnid to tid;
The node.tnid and forum.tid columns have a different purpose.

I think the problem is that one of function/module/code inside the Drupal 7.12 (I dont know yet which one and why) is adding automatically :
INNER JOIN {node} node ON f.tid = node.tid
to the select clause if we're trying to fetch the f.tid field from the inner joined table (forum).

larowlan’s picture

Status: Postponed (maintainer needs more info) » Active

Just to reiterate DO NOT USE THE FIX mentioned at #17.
tnid is for the node translation set
tid is for taxonomy terms

this is not the correct fix, the issue with with node_access rewriting the query and adding in the node table again (it's already in the query) as per comments at #18.

fergusong’s picture

I agree with bak's comment #18.
A little more background. I am trying to learn i18n, and following the tutorial at http://drupal.org/node/1268692
I installed a fairly long list of modules.
I repeated the step earlier today on a second
site and our problem recurred only after install/enabling them. That may narrow things down
a bit. And it may allow others to reproduce the bug.

And I should experiment with selectively disabling these modules to find the culprit, but that
probably won't happen tonight.

larowlan’s picture

Can you grep your code base for functions implementing hook_query_node_access_alter other than node_query_node_access_alter.
The default node_query_node_access_alter would not alter the query as shown at #15 - it would be joining against the node_access table so I am hoping it's another contrib module that's implementing hook_query_node_access_alter and struggling.

fergusong’s picture

Grep, why didn't I think of that, results in
i18n/i18n_select/i18n_select.module: * Implementation of hook_query_node_access_alter().
views/plugins/views_plugin_query_default.inc: // If not, then hook_query_node_access_alter() may munge the count by

The interesting line is the i18n_select.module. AND if I disable i18n_select the error does not
occur, if I enable the module the error returns.

I'll post this as an issue for that module.
There is an old but somewhat related http://drupal.org/node/1065938
And a new post which seems to be reporting the same underlying issue: http://drupal.org/node/1437932
I have added a comment to that post referring to this one and summarizing what we've learned so far.

bak’s picture

fergusong is right. The problem is in the i18n_select.module.
Anybody interested should follow: http://drupal.org/node/1437932

larowlan’s picture

Status: Active » Fixed

Yeeha, we've traced it.
Can everyone affected by this issue confirm they have i18n_select installed?
Marking this as fixed - bot will close it in a couple of weeks - if you aren't using i18n_select then please reopen.

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