Problem/Motivation

I have a page with "Site Statistics" where -among other data- it is shown the number of Nodes the User has voted on. If you, like me, have deleted a good deal of Nodes you may notice that the number of Node votes for a user could be greater than the total amount of Nodes in your site. This is because when deleting a Node, its associated votes still remain.
In my scenario, I'm using the following PHP/SQL to count voted nodes for the current user:
$count_rated = "select count(*) cr from votingapi_vote where uid=$uid;";

Proposed resolution

In order to automatize the cleaning of votes for nodes being deleted, I have created the following Trigger (MySQL in my scenario):

mysql> create trigger delete_vote after delete on node for each row delete from votingapi_vote where content_id=OLD.nid;
Query OK, 0 rows affected (0.05 sec)

When a Node is deleted (DELETE from node...) the Trigger executes deleting the corresponding entries from the Voting API table (votingapi_vote).
Since the above will only work for Nodes deleted after the creation of the Trigger, this is how I deleted the entries for orphaned votes:
mysql> delete from votingapi_vote where content_id not in (select nid from node);

Remaining tasks

The applied solution assumes you have access to your database instance, and you are granted the TRIGGER privilege. In my scenario, I'm the root user of the MySQL instance.
Since this is database dependant, a solution from within the module would be ideal.

Questions for Maintainers

Do you foresee any problem for my solution? Is there any better way to implement this?
For instance, I haven't take into account the vote cache and I don't know how this may impact on the site performance and average counting.

Originally reported by

Many users, mainly with Fivestar Comments, for different versions of Fivestar.
My apologies if this is considered a duplicate.

Comments

whiteph’s picture

Status: Active » Closed (won't fix)

Sorry, Drupal 6 is end of life, and is no longer supported.