Entity query using OR group with the same field name on different relationships doesn't work

Created on 12 August 2022, over 2 years ago
Updated 31 October 2023, about 1 year ago

Problem/Motivation

When using a condition OR group, using two different relationship conditions, but filtering on the same field name on both relationships. You get incorrect results. Only the first condition is properly applied, and not the second.

Steps to reproduce

Node type page has two fields:
field_first_relationship
field_second_relationship

Each one is an entity reference to a different Taxonomy vocabulary (I haven't tried whether this issue exists if both pointing to the same vocab, I assume it will do though).

On both taxonomy vocabs there's a field field_boolean

Entity query:

$query = Drupal::entityQuery('node');
$group = $query->orConditionGroup()
  ->condition('field_first_relationship.entity:taxonomy_term.field_boolean', 1)
  ->condition('field_second_relationship.entity:taxonomy_term.field_boolean', 1);
$query->condition($group);
$results = $query->execute();

Expected results:
I should get back all nodes that have a reference on either field to a taxonomy term with field_boolean: 1

Actual results:
I only get back nodes with a reference on field_first_relationship to a taxonomy term with field_boolean: 1.
field_second_relationship is ignored.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Entity 

Last updated about 5 hours ago

Created by

🇬🇧United Kingdom Leon Kessler

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇦🇺Australia acbramley

    Confirming that #3 still works as a workaround

  • 🇦🇺Australia acbramley

    As per comments in 🐛 Multiple conditions on the same subfield in an entity query fails to join properly Closed: duplicate the issue is with not rejoining on the field table for the 2nd relationship, using the workaround in #3 we can see the correct joins are used and tables in conditions are correctly varied.

    SELECT "base_table"."revision_id" AS "revision_id", "base_table"."id" AS "id"
    FROM
    "proj_personalisation_criteria" "base_table"
    LEFT JOIN "proj_pers_criteria__store" "proj_pers_criteria__store" ON "proj_pers_criteria__store"."entity_id" = "base_table"."id"
    LEFT OUTER JOIN "node" "node" ON "node"."nid" = "proj_pers_criteria__store"."store_target_id"
    LEFT JOIN "node__s_code" "node__s_code" ON "node__s_code"."entity_id" = "node"."nid"
    LEFT JOIN "proj_pers_criteria__stores" "proj_pers_criteria__stores" ON "proj_pers_criteria__stores"."entity_id" = "base_table"."id"
    LEFT OUTER JOIN "node" "node_2" ON "node_2"."nid" = "proj_pers_criteria__stores"."stores_target_id"
    LEFT JOIN "node__s_code" "node__s_code_2" ON "node__s_code_2"."entity_id" = "node_2"."nid"
    WHERE ("node__s_code"."s_code_value" LIKE '%046%' ESCAPE '\\') or ("node__s_code_2"."s_code_value" LIKE '%046%' ESCAPE '\\')
    GROUP BY "base_table"."revision_id", "base_table"."id"
    ORDER BY "base_table"."id" ASC
    LIMIT 50 OFFSET 0
    
  • Status changed to Needs review about 1 year ago
  • 🇮🇳India viren18febS

    #3 🐛 Entity query using OR group with the same field name on different relationships doesn't work Active is working for apply the correct joins & conditions for me.

  • Status changed to Active about 1 year ago
  • 🇬🇧United Kingdom joachim

    Interesting bug!

    > the issue is with not rejoining on the field table for the 2nd relationship, using the workaround

    I wonder whether it's that the relationship data is being formed in such a way that the 2nd relationship clobbers the first one.

    LEFT JOIN taxonomy_term__field_boolean taxonomy_term__field_boolean ON taxonomy_term__field_boolean.entity_id = taxonomy_term_data.tid
    

    The table alias taxonomy_term__field_boolean looks wrong -- it's not containing information on how the taxonomy_term was joined to the base.

  • 🇦🇺Australia acbramley

    @joachim I'm not sure the relationships are overwriting each other, I think it just sees the 2nd field of the same name and skips it (and yeah for some reason the AND groups fix that).

    See the first query I posted in 🐛 Multiple conditions on the same subfield in an entity query fails to join properly Closed: duplicate where there is no second join at all on the field table.

  • 🇯🇴Jordan Anas_maw

    Having the same issue #3 is working for me

Production build 0.71.5 2024