Hello,
let me try to explain my simple relation setup:

Node A {relation} Node B {other_relation} Node C

Node A has a {relation} to Node_B and Node B has an {other_relation} to Node C.

Both relations are optional and I'm not going to require them.

When displaying Node A, I'm going to display fields from Node B and Node C. I'm going to display those fields only if the relationship exists, if they don't exist, the fields are not shown. This is no problem using views.

The Problem:
If I don't require the relationships I get duplicate entries like:

  1. Node A itself
  2. Node A with fields from Node B
  3. Node A with fields from Node B and fields from Node C

The common solution would be to require the relationships but as I also need to display nodes that have no relationships to other nodes I can't go with that. What can I do now to remove the duplicate values?
That means if a relationship form A to B exists, just A alone (1.) without data from B should be no longer displayed.

Comments

hevr’s picture

Database-wise I could think of some ORDER BY and GROUP BY action but this don't seem really feasible because this would need a subquery for ORDERing before GROUPing the subquery.

mikran’s picture

Issue #1506524: Duplicate entries in view with node->node relation was duplicate to this. I also tested this and noticed that with just one relation the duplication won't occur.

osopolar’s picture

On #1506524: Duplicate entries in view with node->node relation I pointed out the important differences between the two sql queries one using the views relationship node->node and the other using two relationships 1:node->relation and 2:relation->node (which is somehow also a node-to-node relationship). Using the first two relationships, I set Position of the relationship base for the first to source and for second to target. For the node->node I only can set source, there is no second option to set the joined node to target. I guess this results in the difference between the two ways to get a node-to-node relationship, one with and the other one without duplicates.

osopolar’s picture

Title: Duplicate entries » Duplicate entries in view with node to node relationships
timb’s picture

I ran into this issue of duplicates appearing in views on a site with one node=>node relation. I did as was suggested in number #3 and was able to remedy this, but it does make the setup less intuitive. I do not think this was an issue until the last update of relation.

marcoka’s picture

i am having the same issue. "require relationship fixes" it but then you are not able to see what node has NO relationship (for example to check who forgot to add one).

jorrit’s picture

I think the query that results when an optional node -> node relation is used is wrong:

SELECT node_node.title AS node_node_title, node_node.nid AS node_node_nid, node.title AS node_title, node.created AS node_created
FROM
{node} node
LEFT JOIN {field_data_endpoints} field_data_endpoints ON node.nid = field_data_endpoints.endpoints_entity_id AND (field_data_endpoints.bundle = 'articles' AND field_data_endpoints.endpoints_entity_type = 'node' AND field_data_endpoints.endpoints_r_index = '0')
LEFT JOIN {field_data_endpoints} field_data_endpoints2 ON field_data_endpoints.entity_id = field_data_endpoints2.entity_id AND field_data_endpoints2.endpoints_entity_type = 'node'
LEFT JOIN {node} node_node ON field_data_endpoints2.endpoints_entity_id = node_node.nid AND (field_data_endpoints2.endpoints_r_index != field_data_endpoints.endpoints_r_index AND 1 = '1' AND field_data_endpoints2.endpoints_entity_type = 'node')
WHERE (( (node.status = '1') AND (node.type IN ('page')) AND (node.nid = '8') ))
ORDER BY node_created DESC
LIMIT 10

I think it should be:

SELECT node_node.title AS node_node_title, node_node.nid AS node_node_nid, node.title AS node_title, node.created AS node_created
FROM
{node} node
LEFT JOIN {field_data_endpoints} field_data_endpoints ON node.nid = field_data_endpoints.endpoints_entity_id AND (field_data_endpoints.bundle = 'articles' AND field_data_endpoints.endpoints_entity_type = 'node' AND field_data_endpoints.endpoints_r_index = '0')
LEFT JOIN {field_data_endpoints} field_data_endpoints2 ON field_data_endpoints.entity_id = field_data_endpoints2.entity_id AND (field_data_endpoints2.endpoints_entity_type = 'node' AND field_data_endpoints2.endpoints_r_index != field_data_endpoints.endpoints_r_index AND 1 = '1')
LEFT JOIN {node} node_node ON field_data_endpoints2.endpoints_entity_id = node_node.nid AND field_data_endpoints2.endpoints_entity_type = 'node'
WHERE (( (node.status = '1') AND (node.type IN ('page')) AND (node.nid = '8') ))
ORDER BY node_created DESC
LIMIT 10

Attached patch achieves this. It also adds a left/right text to the "Avoid left node duplication" checkbox, although I don't really get its function.

I am interested to hear if it also solves your problems.

jorrit’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, relation-optional-views-relations-1444800-7.patch, failed testing.

jorrit’s picture

Status: Needs work » Needs review
StatusFileSize
new18.19 KB

In my opinion, the test was expecting erroneous output. In the case that failed, there was a symmetric relation between two entities (A and B). For a non-required relationship, the expected result was two rows, one with (A, B) and one with (A, NULL). I think this behavior is unwanted: the choice between required and non-required should only affect the inclusion of entities that do not have a relationship of that kind. I have improved the tests by also including tests for a node without any relations (node6). I have split the large test method in relation.views.test so debugging test failures becomes easier.

mikran’s picture

Status: Needs review » Fixed

Awesome, thanks for patch.

jorrit’s picture

Thanks for the commit and git attribution.

Status: Fixed » Closed (fixed)

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