To recreate the problem:

1) Create a nodereference field that has multiple values.
2) Create a table view with only a node title and that nodereference field.
3) Try to view the table.

You will get a message like:

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_fisherman_1 node_data_' at line 1 query: content_views_field_handler_group SELECT node_data_field_fisherman_1.field_fisherman_1_nid AS DISTINCT(node.nid) FROM node node LEFT JOIN content_field_fisherman_1 node_data_field_fisherman_1 ON node.vid = node_data_field_fisherman_1.vid WHERE node.nid = 494

The problem seems to be that content_views_field_handler_group() runs the query through db_rewrite_sql() which is adding DISTINCT around the nodereference nid field, creating the error.

I found a very hackish workaround by adding a dummy nid field to the front of the query. db_rewrite_sql() adds DISTINCT to the dummy field and leaves the nodereference field alone.

function content_views_field_handler_group($field_info, $field_data, $value, $data) {
  $field = $field_info['content_field'];
  $items = array();
  if ($field['multiple']) {
    // Add a dummy nid field to the result to keep db_rewrite_sql() from adding DISTINCT to nodereference nid fields
    $query_columns[] = "nid AS nid1";
    foreach ($field_info['content_db_info']['columns'] as $column => $attributes) {
      $query_columns[] = "node_data_$field[field_name].$attributes[column] AS $column";
    }
    $query = "SELECT ". implode(', ', $query_columns) .
             " FROM {node} node".
             " LEFT JOIN {". $field_info['content_db_info']['table'] ."} node_data_$field[field_name] ON node.vid = node_data_$field[field_name].vid".
             " WHERE node.nid = ". $data->nid .
             " ORDER BY node_data_$field[field_name].delta";
    $result = db_query(db_rewrite_sql($query, 'node'));
 .....

I'm sure there are more elegant solutions, but don't have time to find one right now.

I'm marking this a critical but because grouped multiple value nodereference fields won't work at all in Views without some sort of fix, but there may be disagreement about whether this is critical or not.

Comments

patchak’s picture

Humm I have the same bug here, is there any other solution available, I'm not sure I understand the fix that's described here, and it also seems to me a quite serious bug.

Thanks to anyone who could help me fix this...

Patchak

sovietfunk’s picture

Echo, have this too. The fix works in CCK 5.x-1.6-1, too. For the confused, add these two lines:

    // Add a dummy nid field to the result to keep db_rewrite_sql() from adding DISTINCT to nodereference nid fields
    $query_columns[] = "nid AS nid1";

right after this:
if ($field['multiple']) {
which sits on line 143 in content_views.inc in the CCK module's directory. May be a different line number in other version of CCK.

But this will go poof with a module upgrade, until it is fixed by someone upstream.

mshaver’s picture

This worked for me! It would be great to have a patch submitted for others.

yched’s picture

Status: Active » Fixed

fixed in latest 5.x-1.x : the db_rewrite_sql is not needed to begion with :-/ (we only retrieve nids selected by views' main query, which takes care of the db_rewrite_sql...)

Anonymous’s picture

Status: Fixed » Closed (fixed)

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