Unnecessary Aggregate Rows Created for Non-Existent Entity Fields in votingapi_result

Created on 20 March 2025, 20 days ago

Problem/Motivation

When vote aggregates are calculated and saved, a row is created in the votingapi_result table for every derivative generated by this module. However, this happens even if the field being voted on is not defined for a given entity type.

Consider the following scenario:
field_A exists on node entities.
field_B exists on media entities.
If a vote is cast for field_A, the following rows are created in votingapi_result:

vote_field_average:node.field_A  
vote_field_count:node.field_A  
vote_field_useful:node.field_A  
vote_field_average:media.field_A  
vote_field_count:media.field_A  
vote_field_useful:media.field_A  

Even though field_A is not defined for media entities, rows for media.field_A are still generated. This leads to unnecessary database entries for entity-field combinations that can never have actual data.

Proposed Resolution

Implement hook_votingapi_results_alter() to remove aggregate rows that do not belong to the relevant entity type.

I have a MR incoming.

✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @loze
  • Pipeline finished with Failed
    20 days ago
    Total: 329s
    #453596
  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

    This MR checks if the result field is from the voted entity type.

    I think it should also check if the field is on the bundle and unset the ones that aren't as well.

  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles
  • First commit to issue fork.
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    So ... that means everyone who is using the 8.x-1.x and 2.0.x versions of this module has a bunch of excess rows. If that's the case, then we also need a hook_update_N() function here to clean up the database on existing sites.

    I changed your MR to use the new object-oriented hooks, since everything else in this module and Voting API has already been changed to OO hooks.

  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

    Thanks @tr. yes, this has been happening at least since 8.x-1.x.

    I will try to write an update hook.

  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

    Im also wondering if there is a way to not have the rows generated in the first place, eliminating the need to unset them in the alter hook. Possibly by changing the logic in getDerivativeDefinitions? I will dig into this some more.

  • Pipeline finished with Failed
    19 days ago
    #454712
  • Pipeline finished with Failed
    19 days ago
    Total: 182s
    #454713
  • Pipeline finished with Failed
    19 days ago
    Total: 290s
    #454725
  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

    @td I made a few updates

    1. Checked for a few other scenarios where invalid rows were still being written. It should now only write results for valid entity/bundle/field combos.
    2. Added an update hook that recalculates the results on all voted entities, which cleans up the results table.

    I ran it on my local test site where I have thousands of votes of coming from about 10 different fields across 4 entity types and it cleaned everything up!

  • Pipeline finished with Failed
    19 days ago
    Total: 534s
    #454741
  • Pipeline finished with Failed
    7 days ago
    Total: 178s
    #463998
Production build 0.71.5 2024