i get an error with mysql 5.0.18 (with 4.1 no error) + event module + a node access module (with both node_privacy_byrole and simple_access) and according to http://drupal.org/node/40623 this issue is also with the image module
my log error is:
Unknown column 'n.nid' in 'on clause' query: SELECT DISTINCT(n.nid), e.event_start FROM event e INNER JOIN node_access na ON na.nid = n.nid INNER JOIN node n ON n.nid = e.nid WHERE (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all') OR (na.gid = 0 AND na.realm = 'simple_access'))) AND n.status = 1 AND ((e.event_start > 1136073600 AND e.event_start < 1138751999) OR (e.event_end > 1136073600 AND e.event_end < 1138751999) OR (e.event_start < 1136073600 AND e.event_end > 1138751999)) ORDER BY event_start in /sites-new/includes/database.mysql.inc on line 108.
there is no error when transformed to:
SELECT DISTINCT(n.nid), e.event_start FROM event e INNER JOIN node n ON n.nid = e.nid INNER JOIN node_access na ON na.nid = n.nid WHERE (na.grant_view >= 1 AND ((na.gid = 0 AND na.realm = 'all') OR (na.gid = 0 AND na.realm = 'simple_access'))) AND n.status = 1 AND ((e.event_start > 1136073600 AND e.event_start < 1138751999) OR (e.event_end > 1136073600 AND e.event_end < 1138751999) OR (e.event_start < 1136073600 AND e.event_end > 1138751999)) ORDER BY event_start;
as the problem appears only when i display the calendar on i looked into calendar functions and when i comment the 945 line in event.module displaying the calendar doesnt show any errors
the query at line 945 should be rewritten by db_rewrite_sql or the node access module
thanks for any help pointing me out where this query is rewritten
or maybe a mysql configuration (as it is a mysql5 specific)? i didn't even made any modification to my.cnf
Comments
Samm - do you know which
Samm - do you know which mode your mysql5.0 database was in?
It could be that this is a problem with "strict" mode
http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
--
Wanderlusting Travelers
Knaddison Family
--
Morris Animal Foundation
no mode at all is set mysql>
no mode at all is set
mysql> SELECT @@sql_mode
-> ;
+------------+
| @@sql_mode |
+------------+
| |
+------------+
1 row in set (0.00 sec)
i really dont understand what's wrong, anyway thanks for your suggestions, if you have new ones i'll try out
I think you can fix this
I think you can fix this problem by setting the mode to "MYSQL40". Something like:
SET GLOBAL sql_mode='MYSQL40'
If you have the ability, you could add the
--sql-mode="MYSQL40" to your mysql startup command
Let me know how it goes.
--
Knaddison Family Blog
--
Morris Animal Foundation
It doesn't fix this issue,
It doesn't fix this issue, after SET GLOBAL sql_mode='MYSQL40'; i have:
mysql> SELECT @@sql_mode;
+----------------------------------------------+
| @@sql_mode |
+----------------------------------------------+
| NO_FIELD_OPTIONS,MYSQL40,HIGH_NOT_PRECEDENCE |
+----------------------------------------------+
1 row in set (0.00 sec)
mysql>
anyway i still have the same error
A guess
I've been a little confused by hook_db_rewrite_sql and have been trying to figure it out, because it's a pretty fascinating hook. So I found your post interesting.
As far as I can tell, node_db_rewrite_sql (the node modules implementation of hook_db_rewrite_sql) appears to be the function that inserts the the node_access table in the JOIN clause. What I find interesting is that it also appears to rewrite the entire FROM clause after the first table.
The event.module calls db_rewrite_sql 4 times (note line #'s may not be same as yours but should be close by).
If you look at an error message that has been noted in regards to the event module.
Compare the SELECT and FROM clauses in the error to any of the SELECT and FROM clauses passed to the function, I think it is fairly clear that the 4th call made in the event module (line 1593) is not at issue here, but any of first 3 are a match. Note how
event e INNER JOIN node ngets changed toevent e INNER JOIN node_access na. So the FROM statement was re-written after the first table.It appear that all the reported errors indcate one of those first 3 calls. No one appears to have reported an error that would indicate the last call (line 1593).
The image.module calls db_rewrite_sql 4 times, (note line #'s may not be same as yours but should be close by).
Here's a reported error in regards to the image module
This one is tricky, because of COUNT(*), as none of the SQL strings passed has COUNT(*) in the SELECT clause, but the second one, line 413, is a pager query which would send a COUNT(*). Note the FROM clause passed
FROM {term_node} t INNER JOIN {node} ngets changed toFROM term_node t INNER JOIN node_access na. Again the FROM statement was re-written after the first table.It appears that all the reported errors in regard to the image module point to the second call (line 413) and no one seems to have reported an error that would indicate any of the other 3 calls.
So what's the problem then... I am going to hazard a guess, now that I have seen the SQL statements together. I think the problem may be fixed if when using db_rewrite_sql function, the first table listed in the FROM clause was the NODE table.
So for the event module change the first 3 calls from
SELECT n.nid, e.event_start FROM {event} e INNER JOIN {node} n ON n.nid = e.nidtoSELECT n.nid, e.event_start FROM {node} n INNER JOIN {event} e ON n.nid = e.nid. And for the image module, change the second one (line 413) fromSELECT n.nid FROM {term_node} t INNER JOIN {node} n ON t.nid=n.nidtoSELECT n.nid FROM {node} n INNER JOIN {term_node} t ON n.nid=t.nidNow if that works, I really can't yet explain why exactly, and would love to hear the reason.
I wish I had access to MySQL 5 so I could test my theory, but maybe it will help you. Please let me know if you try it and if it works, or maybe you have already tried it.
i tried your changes quickly
i tried your changes quickly and got thousands of errors, but i don't have a lot of time right now i'll investigate more later
thanks for your help
Killes seems to have the solution
Take look at this (hijacked) thread:
http://lists.drupal.org/pipermail/development/2006-February/013655.html
It seems like the same issue to me and Killes seems to have a solution.
--
Knaddison Family Blog
--
Morris Animal Foundation
Thanks for responding,
Thanks for responding, getting a resolve to this issue is really important to me, I will follow that thread.
More investigation
This seems like a good a place post my findings on the subject. Just to recap, the problem is db_rewrite_sql() is easy to use incorrectly, and msql 5.0.12 and up has changed some of the join rules to conform to the sql2003 standard. (http://dev.mysql.com/doc/refman/5.0/en/join.html, the section that starts with "Note: Beginning with MySQL 5.0.12").
I have found issues in event.module, image.module & taxonomy.module. Three come from not having the $primary_table match the FROM table. This can be fixed by either swapping the join order, or by actively setting the $primary_table parameter of db_rewrite_sql (it defaults to 'n').
Also, there is one case where a USING needs to be changed to an ON clause.
Here are my findings & fixes:
#1: Broken - from event_calendar_data, event_calendar_rss & event_calendar_ical: Unknown column 'n.nid' in 'on clause'
Query: SELECT * FROM {event} e INNER JOIN {node} n ON n.nid = e.nid WHERE n.status = 1. Returned Row Count: 2
Rewritten Query: SELECT * FROM {event} e INNER JOIN {node_access} na ON na.nid = n.nid INNER JOIN {node} n ON n.nid = e.nid WHERE (na.grant_view = 1 AND CONCAT(na.realm, na.gid) IN ('all0','node_privacy_byrole_role1','node_privacy_byrole_user0')) AND n.status = 1. Returned Row Count:
Fix
Query: SELECT * FROM {node} n INNER JOIN {event} e ON n.nid = e.nid WHERE n.status = 1. Returned Row Count: 2
Rewritten Query: SELECT * FROM {node} n INNER JOIN {node_access} na ON na.nid = n.nid INNER JOIN {event} e ON n.nid = e.nid WHERE (na.grant_view = 1 AND CONCAT(na.realm, na.gid) IN ('all0','node_privacy_byrole_role1','node_privacy_byrole_user0')) AND n.status = 1. Returned Row Count: 2
#2: Broken & Throwing Errors - from event_block_upcoming: Column 'nid' in from clause is ambiguous
Query: SELECT * FROM {node} n INNER JOIN {event} e USING (nid). Returned Row Count: 2
Rewritten Query: SELECT * FROM {node} n INNER JOIN {node_access} na ON na.nid = n.nid INNER JOIN {event} e USING (nid) WHERE (na.grant_view = 1 AND CONCAT(na.realm, na.gid) IN ('all0','node_privacy_byrole_role1','node_privacy_byrole_user0')). Returned Row Count:
Fix
Query: SELECT * FROM {node} n INNER JOIN {event} e ON n.nid = e.nid. Returned Row Count: 2
Rewritten Query: SELECT * FROM {node} n INNER JOIN {node_access} na ON na.nid = n.nid INNER JOIN {event} e ON n.nid = e.nid WHERE (na.grant_view = 1 AND CONCAT(na.realm, na.gid) IN ('all0','node_privacy_byrole_role1','node_privacy_byrole_user0')). Returned Row Count: 2
#3: Broken - image_page: Unknown column 'n.nid' in 'on clause'
Query: SELECT n.nid FROM {term_node} t INNER JOIN {node} n ON t.nid=n.nid WHERE n.status=1 AND n.type='image' ORDER BY n.sticky DESC, n.created DESC. Returned Row Count: 2
Rewritten Query: SELECT DISTINCT(n.nid) FROM {term_node} t INNER JOIN {node_access} na ON na.nid = n.nid INNER JOIN {node} n ON t.nid=n.nid WHERE (na.grant_view = 1 AND CONCAT(na.realm, na.gid) IN ('all0','node_privacy_byrole_role1','node_privacy_byrole_user0')) AND n.status=1 AND n.type='image' ORDER BY n.sticky DESC, n.created DESC. Returned Row Count:
Fix
Query: SELECT n.nid FROM {node} n INNER JOIN {term_node} t ON t.nid=n.nid WHERE n.status=1 AND n.type='image' ORDER BY n.sticky DESC, n.created DESC. Returned Row Count: 2
Rewritten Query: SELECT DISTINCT(n.nid) FROM {node} n INNER JOIN {node_access} na ON na.nid = n.nid INNER JOIN {term_node} t ON t.nid=n.nid WHERE (na.grant_view = 1 AND CONCAT(na.realm, na.gid) IN ('all0','node_privacy_byrole_role1','node_privacy_byrole_user0')) AND n.status=1 AND n.type='image' ORDER BY n.sticky DESC, n.created DESC. Returned Row Count: 2
#4: Broken - taxonomy_term_count_nodes (twice): Unknown column 'n.nid' in 'on clause
Query: SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 GROUP BY t.tid. Returned Row Count: 1
Rewritten Query: SELECT t.tid, COUNT(DISTINCT(n.nid)) AS c FROM {term_node} t INNER JOIN {node_access} na ON na.nid = n.nid INNER JOIN {node} n ON t.nid = n.nid WHERE (na.grant_view = 1 AND CONCAT(na.realm, na.gid) IN ('all0','node_privacy_byrole_role1','node_privacy_byrole_user0')) AND n.status = 1 GROUP BY t.tid. Returned Row Count:
Fix
Query: SELECT t.tid, COUNT(n.nid) AS c FROM {node} n INNER JOIN {term_node} t ON t.nid = n.nid WHERE n.status = 1 GROUP BY t.tid. Returned Row Count: 1
Rewritten Query: SELECT t.tid, COUNT(DISTINCT(n.nid)) AS c FROM {node} n INNER JOIN {node_access} na ON na.nid = n.nid INNER JOIN {term_node} t ON t.nid = n.nid WHERE (na.grant_view = 1 AND CONCAT(na.realm, na.gid) IN ('all0','node_privacy_byrole_role1','node_privacy_byrole_user0')) AND n.status = 1 GROUP BY t.tid. Returned Row Count: 1
Glad to know I wasn't
Glad to know I wasn't totally off base on this
I only have a moment right now, but I wanted to share this, from a post I made on the development list (maybe you've already seen it).
I think that the db_rewrite_sql function needs to be more explicit in terms of what sort of SQL statements it accepts for the $query value. I also think that it needs to be very specific in terms of what values are allowed for the $primary_table and $primary_field values.
For example, with node list queries, I think the $primary_table must be 'n', the $primary_field must be 'nid' and the $query value must be in the format SELECT X...X FROM {node} n X...X or there are potential problems.
Looking at the example code posted in drupaldocs for hook_db_rewrite_sql() at http://drupaldocs.org/api/head/function/hook_db_rewrite_sql. The sample asks if table 'n' is the $primary_table and if not adds it to the join clause. If two modules implemented the hook following that example, both checking for the existence of table 'n' and both were enabled on the same site and the $primary_table was 'f' (or 'forum', or 'node' etc. as outlined as acceptable values for db_rewrite_query) the resulting query would be SELECT X...X FROM {forum} f INNER JOIN {node} n ON f.nid=n.nid INNER JOIN {node} n ON f.nid=n.nid X...X which results in ERROR #1066 - Not unique
table/alias: 'n' So, therefore, I also think that any module implementing the hook needs to be aware of potential alias conflicts and should make sure to alias tables with a name that won't conflict with someone else's module.
Here's more information and more thoughts...
Bad interaction with MySQL 5 & Image & Event modules
http://drupal.org/node/40623
SQL error when anonymous: Unknown column 'n.nid'
http://drupal.org/node/46864
I am really happy to help with this, just not sure what to do. Am happy to do more research, to test, to review code, to write code (don't know how to do a patch yet, still a newbie to that)
Thanks
Randall
Not sure I understand
I really need to find some resolution to this as it's causing problems with a site I'm releasing on Tuesday! (See here.) However, I'm not sure I understand this solution.
Do I simply need to enter the rewritten query via phpmyadmin?
You need to fix the query in
You need to fix the query in your php files - most likely the module files.
rjl explained exactly what you need to change...you just need to find where that query is getting created and sent to db_rewrite_sql in the first place.
--
Growing Venture Solutions
Drupal Implementation and Support in Denver, CO
--
Morris Animal Foundation
Ah, okay
So, I simply open up the event.module, find the text that rjl has listed as 'query' and simply replace it with the 'rewritten query.'
Cool. Thanks.