I have a virgin DRUPAL-4-6 CVS install and 4.6 OG module. I am getting the following error when I go to look at the ?q=og page with a non-root user:

user error: Unknown column 'n.nid' in 'on clause'
query: SELECT COUNT(*) FROM og og INNER JOIN node_access na ON na.nid = n.nid INNER JOIN node n ON og.nid = n.nid INNER JOIN users u ON n.uid = u.uid WHERE (na.grant_view = 1 AND CONCAT(na.realm, na.gid) IN ('all0','og_public0','og_all0')) AND og.directory=1 AND n.status=1 in \includes\database.mysql.inc on line 66.

user error: Unknown column 'n.nid' in 'on clause'
query: SELECT DISTINCT(n.nid), n.title, n.body, n.uid, u.name, og.description FROM og og INNER JOIN node_access na ON na.nid = n.nid INNER JOIN node n ON og.nid = n.nid INNER JOIN users u ON n.uid = u.uid WHERE (na.grant_view = 1 AND CONCAT(na.realm, na.gid) IN ('all0','og_public0','og_all0')) AND og.directory=1 AND n.status=1 ORDER BY n.nid DESC LIMIT 0, 50 in \includes\database.mysql.inc on line 66.

Can't seem to duplicate this in my HEAD install.

I will do some more research into what might be causing it and post back.

CommentFileSizeAuthor
#4 og.module_6.patch1.01 KBwebchick

Comments

webchick’s picture

I have two groups created by root:

- Group #1: Invite-only
- Group #2: Open

Each has only one subscriber; root.

My admin >> settings >> og is set to posts only being visible within targeted groups (default).

The authenticated user role only has the default permissions, no create group but should have access content at least.

I don't think this will show up in 4.7/HEAD because node access queries are done completely differently there.

Will step-through with a debugger in an hour or so to try and narrow this down.

jyang’s picture

Title: SQL errors on group view page » SQL errors when enable access control under settings-og-module status
Version: 4.6.x-1.x-dev » 4.7.x-1.x-dev
Component: Code » Miscellaneous

I got the following error each time when I log out from admin, then log in as another user or stay anonymously, the problem come from statistics and "popular content" set up, if I disable both in settings and blocks, the problem is gone. Once I enble it, the problem shows up again.Don't know how to fix it. this also happens to forum module

user warning: Unknown column 'n.nid' in 'on clause' query: SELECT DISTINCT(n.nid), n.title, u.uid, u.name FROM node_counter s INNER JOIN node_access na ON na.nid = n.nid INNER JOIN node n ON s.nid = n.nid INNER JOIN users u ON n.uid = u.uid WHERE (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all') OR (na.gid = 0 AND na.realm = 'og_public') OR (na.gid = 0 AND na.realm = 'og_all'))) AND s.daycount '0' AND n.status = 1 ORDER BY s.daycount DESC LIMIT 0, 3 in C:\progs\web\Apache\Apache2\htdocs\Drupal\drupal-4.7.0-beta4\includes\database.mysql.inc on line 124.

webchick’s picture

Version: 4.7.x-1.x-dev » 4.6.x-1.x-dev
Component: Miscellaneous » Code

jyang: Ok. then please file a different issue about that error, since it is not the same.

This error is coming in @ line 647 in og_list_groups_page:

$result = pager_query(db_rewrite_sql("SELECT n.nid, n.title, n.body, n.uid, u.name, og.description FROM {og} og INNER JOIN {node} n ON og.nid = n.nid INNER JOIN {users} u ON n.uid = u.uid WHERE og.directory=1 AND n.status=1 ORDER BY n.nid DESC"), 50);

The original query that gets passed in executes okay. But the final query that pager_query executes is:

SELECT COUNT(*) FROM {og} og  
INNER JOIN {node_access} na ON na.nid = n.nid 
INNER JOIN {node} n ON og.nid = n.nid 
INNER JOIN {users} u ON n.uid = u.uid 
WHERE  (na.grant_view = 1 AND CONCAT(na.realm, na.gid) IN ('all0','og_public0','og_all0'))
AND  og.directory=1 AND n.status=1

(I've thrown line breaks in here so it's easier to read)

Notice that node_access is trying to do a join on n.nid before the node table has been added. If you swap lines 2 and 3 of that query, it executes fine.

The reason this query doesn't fail for the 'root' user is because it skips the node_access check altogether, and only passes the following into pager_query():

SELECT COUNT(*) FROM {og} og INNER JOIN {node} n ON og.nid = n.nid INNER JOIN {users} u ON n.uid = u.uid WHERE og.directory=1 AND n.status=1

Any ideas how I can fix the order of the table joins in OG's rewrite query?

webchick’s picture

Title: SQL errors when enable access control under settings-og-module status » SQL errors on group view page for non-superusers
Status: Active » Needs review
StatusFileSize
new1.01 KB

Thanks to merlinofchaos (and chx), here is a simple patch to fix this! It simply reverses the order of the og and node tables in the pager query.

The reason this is needed is (paraphrasing) because db_rewrite_sql stuff is only programmed to react when the primary table is node; here it was set to 'og' instead.

jyang’s picture

I vreate anew issue under:
http://drupal.org/node/51842
Please follow up over there. By the way, I looked at your patch, it seem to me the version of OG is not the version I am using, i.e. the latest release.
Thanks

webchick’s picture

Yep, this issue is for the 4.6 version of OG module, that's why it's better to put your issue in a separate bug report. :) Thanks for doing that. I'll try and take a look.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

please see if other versions need similar fix. RTBC. thanks.

webchick’s picture

Component: Code » og.module
Status: Reviewed & tested by the community » Fixed

Committed to 4.6 branch, thanks!

I did try the same fix against 4.7/HEAD (since its pager query is still done with the og table in front of node) and it ended up breaking the page horribly. Apparently this has to do with HEAD issuing node_access queries based on a different sort order than 4.6 does. For instance, 4.6 doesn't seem to suffer from the statistics module bug that the 4.7 version does. Weirdness. :P

Also, just so this is noted here, apparently this is only an issue that's cropped up in MySQL 5 and the way it does joins. But it doesn't hurt anything in previous versions.

Anonymous’s picture

Status: Fixed » Closed (fixed)