I'm getting a big red error when I go to my site.

I'm clueless about Drupal and have no idea what to do.

user warning: Unknown table 'n' in where clause query: SELECT DISTINCT(node.nid), node.type AS node_type, node.title AS node_title, node.changed AS node_changed, users.name AS users_name, users.uid AS users_uid, node.created AS node_created, node_counter.daycount AS node_counter_daycount FROM node node LEFT JOIN users users ON node.uid = users.uid LEFT JOIN node_counter node_counter ON node.nid = node_counter.nid INNER JOIN node_access na ON na.nid = node.nid WHERE (na.grant_view >= 1 AND ((na.gid = -1 AND na.realm = 'all') OR (na.gid = 0 AND na.realm = 'og_public') OR (na.gid = 0 AND na.realm = 'og_all') OR (na.gid = 3 AND na.realm = 'og_subscriber') OR (na.gid = 4 AND na.realm = 'og_subscriber') OR (na.gid = 0 AND na.realm = 'cac_lite'))) AND (n.moderate != 1) AND (node.status = '1') ORDER BY node_counter.daycount DESC LIMIT 0, 5 in /home/orbit42/public_html/includes/database.mysql.inc on line 120.

Comments

admin@thanetcouncil.info’s picture

Component: node system » base system

It seems to only effect not logged in users.

n.moderate != 1

it seems to be a core issue.

I've read lots of "nodes" on the subject of similar errors it seems to be a bug so utterly intrinsic to druple as to make even a mildy interest in php soul utterly unable to solve the porblem.

For me this issue is utterly critical and a large project is now stuck.

I had hoped it was cck related but removing all that didn't help

http://drupal.org/node/73551 (I got the idea here)
http://drupal.org/node/51850 suggests it's a Mysql 5.x issue which it is not and blames statistics.module
http://www.civicspacelabs.org/home/node/16669 points to the former and suggests there is a "patch but being a worthless windows user I have no idea what to do with it and being just some guy with shared hosting I can not execute scripty commands in supergeekosgodspeak or whatever.
http://drupal.org/node/51842 blames OG

So let's see it is collectivly caused by SSK or stastics or OG or something else.

I get the idea no one actually knows what is wrong or why and given this issue crops up at least as early as March 1, 2006 no one knows how to fix it.

I'm starting to feel a little [1] angry and dearly wish there was someone to be angry at (we don't get everything I guesS).

Google produces 631,000 results with the search :: Unknown table 'n' in where clause query

most of these are drupal sites with the error or "branches" talking about it.

The highest rated site with the error is this one http://www.iias.nl/host/abia/node/59

"user error: Unknown table 'na' in where clause
query: SELECT n.nid FROM event e INNER JOIN node n ON e.nid = n.nid WHERE (na.gid = 59 AND na.realm = 'og_group') AND n.status = 1 AND e.event_start BETWEEN 1157068800 AND 1159660799 in /iias/host/abia/includes/database.mysql.inc on line 66."

http://drupal.org/node/50165 this might have something to do with it too? [2]

I thought it might be event [2] but after uninstalling it ...nope - so what do I know?

[1] This is typical British understatement
[2] As a windows user I obviously know nothing

admin@thanetcouncil.info’s picture

http://www.gerd-riesselmann.net/archives/2005/11/one-feed-module-to-rule...

No one knows... is DRUPAL DEAD?

Now that has a ring to it. I'm blogging under that heading in 7 days.

greggles’s picture

What modules do you have installed on your site?

admin@thanetcouncil.info’s picture

still enabled:

actions
advanced_menu
aggregator2
aggregator2_autotaxonomy
aggregator2_logo
... others...

I tried to uninstall them all and got this error

Fatal error: Call to undefined function: category_get_container() in /home/orbit42/public_html/modules/taxonomy.module on line 740

now ~I can not access the modules section

Fatal error: Call to undefined function: category_get_container() in /home/orbit42/public_html/modules/taxonomy.module on line 740

DRUPAL IS DEAD.

Lets just face facts no one knows what causes the first error and no my install is broken and I don't know how to fix it. All my users data is gone.

It's hopeless.

there is no point.

sorry to waste your time.

cog.rusty’s picture

These error messages come from category module, from its included replacement taxonomy module (in /wrappers), and from an access control module (either cac_lite included in category module or another access control module).

Category module deviates from standard Drupal in several ways. I would start my hunt by asking its developer, just in case he is around and he is not onto something else.

Oh, and I would try to keep to the point and be as technical as I can.

killes@www.drop.org’s picture

Project: Drupal core » Category
Version: 4.7.3 » 4.7.x-1.x-dev
Component: base system » Code

moving

Jaza’s picture

Are you using the views module? The problem query that you posted looks to me like it could be views-generated. I don't think category is responsible for this - it doesn't do query generation, and there is no query in the module that looks like that.

cog.rusty’s picture

What made me mention category's taxonomy wrapper was the "category_get_container()" error in #4.

I also notice that the query in the initial post mentions both cac_lite and og (og_public and og_all), possibly a problematic combination.

gerd riesselmann’s picture

Seems I'm somehow involved in this (detected the link), though I'm not sure how excately. Well here are my two cents... Rather some mumbling and loud thinking than a concrete solution, though.

An error like this usually is caused by some module "rewriting" a query of some other module, each of them not aware of each other. This is a powerful feature, yet it also is highly error prone - as the original poster already figured out.

The process of rewriting a query works like this: A module builds a SQL query and passes it to the function db_rewrite_sql, together with the table name and some other information.

In general there are two possible error sources:

  • A module invokes db_rewrite_sql with wrong parameters
  • A module wrongly implements the actual rewriting

Each of these errors will occur only if the query is rewritten, which actually may depend on a the module configuration, user state etc. That's why there are so much different forms of the error.

That's also the reason why the list of modules installed is extremly important with issues like this.

Regarding the concrete sql query causing the error, two modules obviously are of different opinion, how the node table should be named. While the invoking module uses "node" as alias, the module appending "n.moderate != 0" assumes "n".

For me this looks like either one the following has happened, though this is only a - not even educated - guess.

  • The invoking module uses "node", which is correct, following the documentation, but which seems unusual looking at the code (which says "n"). However, the module maybe invokes the rewriting omitting the table name parameter, so "n" gets filled in, leading to the modules doing the rewrite assuming "n" as alias.
  • Or: The module rewriting the query simply ignores the table name passed

What I would do is to search all the modules installed for portions of the SQL string. This is to figure out the modules involved in producing this query. I did this for my system and the only module I found using node as table alias is the view module. I'm not sure, but most likely, this also is the source of the query that is causing you trouble.

The view module however invokes the function correctly and additionally some of the rewriting (regading the access check) seems to be done correctly ("INNER JOIN node_access" comes from node module, I think), so I would for now concentrate on possibilty 2, that the rewriter is doing wrong. Try to find "_db_rewrite_sql" in the sources to find all places where a query gets rewritten. These are to be examined if they add the "moderate" check. But note that searching is not possible using windows search, since Windows search will ignore the module files (as unknown file type).

Again: The list of modules installed is extremly important to fix this bug.

Btw: As far as I can see, none of the category modules is involved in this error.

cog.rusty’s picture

Project: Category » Views (for Drupal 7)

Interesting observations. I would pass the ball to views, then back to core, and if all fails it will probably remain unresolved for lack of a complete module/version list.

merlinofchaos’s picture

I agree with gerd; this is neither a views issure nor a core issue. Views doesn't put items into a query in the form of'n.moderate = 1; it would instead be node.moderate = '1'.

Still, it might be worthwhile to get an export of whatever view is causing this, just to ensure that there isn't some funny filter somewhere.

The only way to track this down is to get a complete module list of every contributed module you have active in your system.

However, in doing a grep on the entire modules directory (and you can THANK me for that piece of work) I believe your problem is likely the 'og_moderate' module.

You can confirm this by disabling that module.

merlinofchaos’s picture

Status: Active » Fixed

I have submitted a patch to the og_moderate module that should fix this condition.

http://drupal.org/node/83267

gerd riesselmann’s picture

Status: Fixed » Active

Well, allowing every module to change the sqls of any other module doesn't necessarily ensure a controlled environement. Welcome to support hell :-)

I tried to tell people where and what for to look (teach a man to fish...). And I continue the mission with some more details:

Code like this (a patch taken from here) illustrates the problem. It is a true troublemaker:

/**
 * Implementation of hook_db_rewrite_sql
 */
function content_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
  global $user;
  switch ($primary_field) {
    case 'nid':
      if ($user->uid != 1) {
        $return = array();
        $where = array("n.type <> ''");
        foreach (content_types() as $name => $type) {
          if (!user_access('view '. $name .' content')) {
            $where[] = "n.type <> '$name'";
          }
        }
        $return['where'] = join(' AND ', $where);
        return $return;
     }
     break;
  }
}

Note that this codes checks if the element queried is a node. Therefore it looks if the query deals with a node's id (case "nid"). This is totally correct.

But then the code assumes the table alias is "n" - for exampel in this line: $where = array("n.type <> ''");. Notice "n.type"? This is plain wrong, and will be causing an "unknown table n" error, if the table alias is "node" - as is the case with the problem discussed here.

Correct code would use the table alias $primary_table, like so:$where = array($primary_table . ".type <> ''");. Here's the whole snippet, corrected:

/**
 * Implementation of hook_db_rewrite_sql
 */
function content_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
  global $user;
  switch ($primary_field) {
    case 'nid':
      if ($user->uid != 1) {
        $return = array();
        $where = array($primary_table . ".type <> ''");
        foreach (content_types() as $name => $type) {
          if (!user_access('view '. $name .' content')) {
            $where[] = $primary_table . ".type <> '$name'";
          }
        }
        $return['where'] = join(' AND ', $where);
        return $return;
     }
     break;
  }
}

This little error in the implementation of the db_rewrite_sql-hook is not noticed, as long as the primary table is "n", which it is in about 95% of all cases. This turns the code into a neat little timebomb.

There are hundreds of modules, and any of them may contain a wrong implementation like the above one. Additionally there are hundreds of patches, which - like the example - may contain errors, too.

So, my advise in case of trouble is to look for code fragments like above. Look for functions named "[modulename}_db_rewrite_sql" and see if there's a hardcoded "n.[fieldname]" in it.

gerd riesselmann’s picture

Oops, took to long to write my prior comment. It partly answers to #10.

I think merlinofchaos is right and his patch proposed in comment #12 will fix the problem.

merlinofchaos’s picture

Status: Active » Fixed

gerd -- you deserve a real round of applause for that bit of sleuthing. Good job. You were absolutely correct in every detail.

The patch I linked has already been committed, so an update of og_moderate (once the new tarball is generated in the next 12 hours or so) will also fix the problem.

Anonymous’s picture

Status: Fixed » Closed (fixed)