While updating a module for D6, I ran into an error in my hook_db_rewrite_sql where the primary table is the comments table ('c'):

user warning: Unknown table 'c' in where clause query: SELECT COUNT(*) FROM comments...

This is because of the following in the comment_render function in comments.module:

      $query_count = 'SELECT COUNT(*) FROM {comments} WHERE nid = %d';
      ...
      $query_count = db_rewrite_sql($query_count, 'c', 'cid');

So I've attached a patch that adds the 'c' table alias to the $query_count variable.

Comments

mcarbone’s picture

StatusFileSize
new1.15 KB

Oops, I missed a spot to add the alias. New patch attached.

mcarbone’s picture

Title: Missing table alias in comment_render function » Missing table alias in comment_render function breaks hook_db_rewrite_sql

This is a very tiny fix -- without it, hook_db_rewrite_sql cannot work with the comments table. It should be easy to review.

webchick’s picture

Can confirm the bug, and the fix. Moving to critical, since it breaks db_rewrite_sql() for comments.

To reproduce, stick the following in your module and view a node page with some comments as a non-admin user:

function example_db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid', $args = array()) {
  if ($primary_field == 'cid') {
    $return['join'] = "INNER JOIN {comments} c2 ON $primary_table.cid = c2.cid";
    $return['where'] = "$primary_table.status = c2.status";
    return $return;
  }
}

First problem:

user warning: Unknown column 'c.status' in 'where clause' query: pager_query /* peon : pager_query */ SELECT COUNT(*) FROM comments INNER JOIN comments c2 ON c.cid = c2.cid WHERE (c.status = c2.status) AND ( nid = 1 AND status = 0) in /Applications/MAMP/htdocs/6x/modules/comment/comment.module on line 989.

(fixed w/ mcarbone's patch)

Second problem:

user warning: Column 'nid' in where clause is ambiguous query: pager_query /* peon : pager_query */ SELECT COUNT(*) FROM comments c INNER JOIN comments c2 ON c.cid = c2.cid WHERE (c.status = c2.status) AND ( nid = 1 AND c.status = 0) in /Applications/MAMP/htdocs/6x/modules/comment/comment.module on line 989.

fixed with this one, which includes mcarbone's patch.

Also tested recent comments block, etc. and this appears to be working. Could use a couple sets of eyes, though.

webchick’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new1.71 KB

Ressin' fressin. :P

webchick’s picture

Priority: Normal » Critical
webchick’s picture

Version: 6.x-dev » 7.x-dev
StatusFileSize
new1.15 KB

Btw, here's one that applies to 7.x without the off-set and all the svn crap at the end. Same patch.

webchick’s picture

Status: Reviewed & tested by the community » Needs review
dries’s picture

Status: Needs review » Fixed

I've committed this to CVS HEAD but it should probably go into DRUPAL-6 too.

mcarbone’s picture

Version: 7.x-dev » 6.x-dev
Status: Fixed » Needs review

Re-opening the issue to be applied to Drupal 6.

KingMoore’s picture

Status: Needs review » Reviewed & tested by the community

RTBC patch coming soon.

webchick’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.15 KB

Here's an updated patch that applies cleanly to 6.x.

realityloop’s picture

Status: Needs review » Reviewed & tested by the community
KingMoore’s picture

RTBC for real.

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

Thanks, committed to Drupal 6 too.

Anonymous’s picture

Status: Fixed » Closed (fixed)

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