I created a node reference field with multiple values. Everything is OK when view/edit the content type that has the filed. Then I created a table view containing the filed (with default settings) and got errors like following.
user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT(node.nid) FROM node node LEFT JOIN content_field_contacts node_data_fie' at line 1 query: SELECT node_data_field_contacts.field_contacts_nid AS DISTINCT(node.nid) FROM node node LEFT JOIN content_field_contacts node_data_field_contacts ON node.vid = node_data_field_contacts.vid WHERE node.nid = 223 ORDER BY node_data_field_contacts.delta in E:\Web\drupal\includes\database.mysql.inc on line 172.
If I unchecked "Multiple values" of the node reference filed, there was no error.
MySQL database 4.1.22
Web server Apache/2.0.59 (Win32) PHP/5.2.1 DAV/2
drupal 5.1
views version = "5.x-1.6-beta5"
cck version = "5.x-1.5"
Comments
Comment #1
adrienf commentedI have exactly the same error!!!
Comment #2
merlinofchaos commentedThe query that's failing does not look like a Views query. (Worse it looks like Yet Another Bad rewrite from Drupal core.
Comment #3
yched commentedhttp://www;drupal.org/node/163652 was marked duplicate - below is the text from this other thread, as it has some detailed info.
To reproduce this bug, create a view with several fields including a nodereference. Then use it as the advanced view to select reference items in another nodereference field. Let's say this field is used in a content type called "thingy".
You should see a mysql error on both the create content page for thingy and on the manage fields page. I haven't looked into it in detail but the problem seems to be related to SQL rewrite.
One thing in passing - when generating the widgets for the manage fields page it would surely be better if there were some flag that told the nodereference module not to bother getting the reference objects. That's the page I first saw the error on, and I was extremely puzzled as to why that page seemed to be fetching nodes, until I dived into the code and it saw what was going on.
Comment #4
alfaguru commentedI tracked down the code responsible for this error, though I don't have a proper solution as yet. It's in content_views.inc, in the function content_views_field_handler_group().
The lines in question are:
The call to db_rewrite_sql() results in erroneous SQL being generated. I've worked round this by removing it, so the final line above becomes simply:
Losing the call to db_rewrite_sql() is unlikely to cause an issue in most cases, but it would be much better to fix the input SQL so it is rewritten correctly.
Comment #5
jonphillips commentedThanks a lot for that "temporary" solution, alfaguru ... worked for me!
Jon
Comment #6
ica commentedHi,
just to confirm here that i had exactly the same errors -and the fix worked
thanks for posting this issue and the -quick fix
hope the issue will be fixed soon as the nodereference.module and CCK are a popular widely used Drupal modules as its a crutial bug
Comment #7
karens commentedI'm now running into this error as well. It is indeed a problem with db_rewrite_sql() which grabs the first 'node.nid' it can find and makes it DISTINCT, which breaks badly with nodereference. There are two ways to fix this. One is the one mentioned above, to avoid using db_rewrite_sql() at all. The other is to give the rewrite something else to attach its behavior to, which can be done by adding 'node.nid, '. to the beginning of the list of fields so that it is the first item in the select list.
Both fixes will work, the question is whether we need to be using db_rewrite_sql() or not. If so, the second fix is better. I'm going to commit the second fix. If that seems not to be the right solution, this issue can be re-opened.
Comment #8
yched commentedThe node.nid trick definitely seems better than removing db_rewrite_sql. Cool :-)
Comment #9
merlinofchaos commentedHmm. I wonder if using something like:
would fix some of the problems in Views, where db_rewrite_sql gets really twitchy if node.nid is already distinct.
Comment #10
(not verified) commented