Steps to reproduce:

  1. Create a content type with a node reference field
  2. Create a view that uses a nodereferrer relationship and lists the nodes that are referred to by a particular node id (see screenshot attached)
  3. Create a new node that features a node reference
  4. Make a standard node revision and remove the node reference
  5. Note that the relationship is still shown in the view even though the nodereference has been removed during the last revision

Am I doing something wrong? Do I need to build my view as a "node revision" type instead of a "node" type?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

miro_dietiker’s picture

Hello

I've been working on this issue (for a long time)... it took me an unexpected amount of time since all relationship samples found don't trigger this issue.
Indeed nodereferrers view integration is broken. As long as you don't have node_access activated, you even will have N duplicate lines. Since every vid (revision) writes node references again, the sum of all revision entries will be shown as references. Node access finally tops the issue because it adds a DISTINCT without asking for it.. Leading to the result and us to expect everything is right till deletion occur and nothing disappears.

Technically i'll show you this based on forward direction of node reference.

--------    ---------------   --------
|Node  |    |Field        |   |Node  |
|Source|    |NodeReference|   |Target|
|      |    |             |   |      |
|nid   |--->|nid          |   |      |
|vid   |===>|vid          |   |      |
|      |    |target_nid   |==>|nid   |
|      |    |             |   |vid   |
--------    ---------------   --------

First join is always vid based, so old revisions get lost. The second join to the target node table is nid based so referenced node for current revisions will show up.

If you reverse the relationship the following will occur:

--------    ---------------   --------
|Node  |    |Field        |   |Node  |
|Source|    |NodeReference|   |Target|
|      |    |             |   |      |
|      |    |nid          |<==|nid   |
|vid   |    |vid          |   |vid   |
|nid   |<===|target_nid   |   |      |
--------    ---------------   --------

Leading to vid multiplication in result (also for ancient references that are currently being deleted).
Correct would be:

--------    ---------------   --------
|Node  |    |Field        |   |Node  |
|Source|    |NodeReference|   |Target|
|      |    |             |   |      |
|      |    |nid          |<--|nid   |
|vid   |    |vid          |<==|vid   |
|nid   |<===|target_nid   |   |      |
--------    ---------------   --------

The referrer handler needs to redefine not only ensure_my_table() but also query() with tiny changes to the views' default relationship handler. Simply copy the query from views' content_handler_relationship.inc and change the lines below marked as "changed".

<?php
  /**
   * Called to implement a relationship in a query.
   */
  function query() {
    // Figure out what base table this relationship brings to the party.
    $table_data = views_fetch_data($this->definition['base']);

    $this->ensure_my_table();

    $def = $this->definition;
    $def['table'] = $this->definition['base'];
    $def['field'] = 'vid'; // changed
    $def['left_table'] = $this->table_alias;
    $def['left_field'] = 'vid'; // changed
?>

Since i'm working on a correct node-reference-reverse views integration module only without nodereferrer cck fields overhead, i'm short in time for providing a nodereferrer patch. Please build one and integrate it in the project.
Feel free to contact me for support.

miro_dietiker’s picture

I have to admit that this solution still isn't perfect. There is still a Problem when not requiring relationships: duplicates may result in the base table.

See #241078: Reverse node-reference views relationship for more detail.

attheshow’s picture

Category: support » bug

Here's the problem with my particular query that's being generated by Views. I'm not sure how to update the module's Views integration to get the root issue fixed.

Original Query:
SELECT node.nid AS nid,
node_node_data_field_course.title AS node_node_data_field_course_title,
node_node_data_field_course.nid AS node_node_data_field_course_nid,
node_node_node_data_field_sort_order.field_sort_order_value AS node_node_node_data_field_sort_order_field_sort_order_value,
node_node.nid AS node_node_nid,
node_node.type AS node_node_type,
node_node.vid AS node_node_vid,
node_node.title AS node_node_title
FROM cms_node node
LEFT JOIN cms_content_field_criteria node2 ON node.nid = node2.field_criteria_nid
INNER JOIN cms_node node_node ON node2.nid = node_node.nid
LEFT JOIN cms_content_field_course node_node_node_data_field_course ON node_node.vid = node_node_node_data_field_course.vid
INNER JOIN cms_node node_node_data_field_course ON node_node_node_data_field_course.field_course_nid = node_node_data_field_course.nid
LEFT JOIN cms_content_type_course_objective node_node_node_data_field_sort_order ON node_node.vid = node_node_node_data_field_sort_order.vid
WHERE node.nid = 68279
ORDER BY node_node_data_field_course_title ASC, node_node_node_data_field_sort_order_field_sort_order_value ASC

Query should be:
SELECT node.nid AS nid,
node_node_data_field_course.title AS node_node_data_field_course_title,
node_node_data_field_course.nid AS node_node_data_field_course_nid,
node_node_node_data_field_sort_order.field_sort_order_value AS node_node_node_data_field_sort_order_field_sort_order_value,
node_node.nid AS node_node_nid,
node_node.type AS node_node_type,
node_node.vid AS node_node_vid,
node_node.title AS node_node_title
FROM cms_node AS node
LEFT JOIN cms_content_field_criteria node2 ON node.vid = node2.vid
INNER JOIN cms_node node_node ON node2.nid = node_node.nid
LEFT JOIN cms_content_field_course node_node_node_data_field_course ON node_node.vid = node_node_node_data_field_course.vid
INNER JOIN cms_node node_node_data_field_course ON node_node_node_data_field_course.field_course_nid = node_node_data_field_course.nid
LEFT JOIN cms_content_type_course_objective node_node_node_data_field_sort_order ON node_node.vid = node_node_node_data_field_sort_order.vid
WHERE node2.field_criteria_nid = 68279
ORDER BY node_node_data_field_course_title ASC, node_node_node_data_field_sort_order_field_sort_order_value ASC

lunk rat’s picture

I have this same problem. Node referrer values remain in the view even after the nodereference field has been changed/revised.

Any fix for this?

What does this have to do with Node Access?

Thanks!

sillygwailo’s picture

Version: 6.x-1.0-beta1 » 6.x-1.0-rc1

Bumping up the version number, since we seem to have the same issue in RC1.

sillygwailo’s picture

Patch based on the comment in comment #3 is attached.

zilverdistel’s picture

Patch in #6 seems to work for me. Thanx Richard!

(must be applied in nodereferrer/views)

Cyberwolf’s picture

Patch from #6 seems to solve the problem here. Thanks!

andypost’s picture

Version: 6.x-1.0-rc1 » 6.x-1.x-dev
Status: Active » Reviewed & tested by the community

Please, re-roll a patch against current CVS so I can commit #6

andypost’s picture

Please, re-roll a patch against current CVS so I can commit #6

sillygwailo’s picture

The patch appears to apply cleanly to the current dev from CVS. Attaching the re-roll anyway, with an updated path to the file to be patched.

sillygwailo’s picture

Status: Reviewed & tested by the community » Needs review
andypost’s picture

Status: Needs review » Fixed

Thanx, Commited http://drupal.org/cvs?commit=464392 Dev tarball should be regenerated soon.

miro_dietiker’s picture

Status: Fixed » Needs work

The current solution is much cleaner than before.
However i think that we where sure that a perfectly clean solution can only be reached if USING A SUBQUERY.
However this might only match for the reverse query case.

e.g. if you don't force a relationship, the query still contains an INNER join somehow. So if there's no relation persisted, we will loose some source lines.

This needs thorough testing.

andypost’s picture

Suppose having this commited into DEV we get more feedback about this change