When updating an existing comment I get the following error:

Drupal\Core\Database\IntegrityConstraintViolationException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '52' for key 'PRIMARY': INSERT INTO {comment_notify} (cid, notify, notify_hash) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 52 [:db_insert_placeholder_1] => 0 [:db_insert_placeholder_2] => qECk4_EAzImRJJ3bvY9eogcdEyeac-iqJTVjcNRtbO8 ) in Drupal\Core\Database\Connection->handleQueryException() (line 668 of /var/www/html/core/lib/Drupal/Core/Database/Connection.php).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Polly created an issue. See original summary.

gnuget’s picture

Can you please provide a list of steps to reproduce the problem?

Thanks.

greggles’s picture

Status: Active » Postponed (maintainer needs more info)

Better status for something where we need more info.

RobertoGuzman’s picture

i have the same error every time i'm updating an existing comment

greggles’s picture

@RobertoGuzman, can you provide a set of steps to reproduce like:

1. Install Drupal 8
2. Configure X
3. Post a comment
4. Edit the comment
5. Observe the error message

RobertoGuzman’s picture

I post a comment, i saved it

I edit the comment, when i save it, i get the error

pcranston’s picture

I have the same issue as @Polly and @RobertoGuzman. My situation:

1. Drupal 8.3.7, 8.x-1.x-dev
2. Configure Comment Notify for a certain content type, nothing special here
3. Post a comment, with "Notify me when ..." set to unchecked
4. Edit that comment, leave "Notify me when ..." as unchecked, click save
5. Observe error message about comment notify integrity constraint violation

The problem is line 140:

if (comment_notify_get_notification_type($comment->id())) {
    // Update existing record.
    comment_notify_update_notification($comment->id(), $status);
  }

If you unchecked the comment notify when submitting the comment, then comment_notify_get_notification_type() returns the value of notify = 0, and this if condition is not matched. In other words, it's checking whether it's set to on or off, not whether the database record exists, which is what it should be doing here. The code then proceeds to the comment_notify_add_notification() insert at line 153.

The attached patch as an additional function, comment_notify_get_notification_id() which checks whether the database record exists at all, regardless of the value of the 'notify' column, and will return the correct value to the submit function.

nevergone’s picture

Status: Postponed (maintainer needs more info) » Needs review
FileSize
1.11 KB

Re-rolled patch.

gnuget’s picture

Status: Needs review » Needs work
+++ b/comment_notify.inc
@@ -184,6 +184,20 @@ function comment_notify_get_notification_type($cid) {
+  return db_select('comment_notify', 'cn')

Can we stop using db_select? and start using the new API?

Thanks!

nkoporec’s picture

Status: Needs work » Closed (duplicate)
Related issues: +#2877283: Integrity constraint violation on anonymous's user comment update

This issue is related to Integrity constraint violation on anonymous's user comment update, which has a working patch.

greggles’s picture

I committed that and created a new release at https://www.drupal.org/project/comment_notify/releases/8.x-1.0-beta2

For those following this issue I hope you will test it out and confirm if it works for you and confirm if this issue is a duplicate of that.