Hello,

Here the issue:

1. Logged in user flags some content
2. Same user comes back later and is not logged in.
3. Flags same content as in step one
4. then remembers that he is not logged in and does so.
5. sql error for duplicate entry

Is there a way to avoid this? The best way would be to merge the anonymous user entry and the logged in one.

Should you need more infos then i would be happy to help.

regards

Wolfmarter

Comments

wolfmarter’s picture

Priority: Normal » Major

I have not been able to figure out how to merge the database values on loginn

i will keep trying and report my efforts

maybe someone can help??

thx wolfmarter

quicksketch’s picture

Title: Login with duplicate entry » Logging in after anonymous flagging may cause duplicate entry SQL errors
Priority: Major » Normal

Thanks for the report, sounds like a likely issue and no further information should be needed. I'll test this out and see if I can reproduce it.

wolfmarter’s picture

that would be wonderfull since i am in the need of this function for a current project. If i can help you in any way i will be glad to do so.

regards

wolfmarter

quicksketch’s picture

I was looking in to this issue today and it looks like we already attempted to solve this problem by handling it the same as we did in Drupal 6. The applicable code already says it's trying to avoid these errors, but obviously the same approach we used before doesn't work in Drupal 7. Here's the code that is causing the error (from flag.module, flag_user_login()):

    // The @ symbol suppresses errors if the user flags a piece of content
    // they have already flagged as a logged-in user.
    db_update('flag_content')
      ->fields(array(
        'uid' => $account->uid,
        'sid' => 0,
      ))
      ->condition('uid', 0)
      ->condition('sid', $sid)
      ->execute();

Because the new database system throws an exception (not just a PHP warning), we'll need to figure out a different way to handle this.

quicksketch’s picture

Status: Active » Fixed
StatusFileSize
new2.94 KB

This patch corrects this issue on Drupal 7 by using a similar approach but doing each UPDATE query individually. Because a single warning will cause the entire update to fail in Drupal 7 (differences in how PDO works from normal queries it seems), we have to use this less efficient but effective approach.

I also discovered an issue related to this of database counts causing SQL errors. This was due to our dependence on db_update() returning a count of changed rows, but in some situations we would call the _update_count() method with a count that matched the current count, leading to a SQL error when Flag tried to do an INSERT when a row already existed. While this was a common practice in D6, in D7 we have the convenient db_merge() function to handle this exact situation, so I've used that in this patch to fix the additional problem.

I've committed this patch to the 7.x-2.x branch. The D6 branch is not affected by these problems.

Status: Fixed » Closed (fixed)

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

Ivan Simonov’s picture

Status: Closed (fixed) » Active

quicksketch, are you sure this patch was committed to the 7.x-2.x branch?

St_B’s picture

Issue summary: View changes

Hello, is there any version of this patch for flag 7.x-3 branch ?

joachim’s picture

Status: Active » Closed (fixed)

The 7.x-3.x branch was forked from the 7.x-2.x branch long after this issue was closed! You can see the code from this patch, albeit with some changes for the 3.x table names, in flag_user_login().

St_B’s picture

@joachim ok, we thought the error we have looks pretty like what is described in this issue.

step 1 : anonymous user create a flagging object ; this flagging can also be flagged by another user
step 2 : admin user flags the previous flagging => error : Integrity constraint violation: 1062 Duplicate entry '3140' for key 'PRIMARY': INSERT INTO {flagging}
It seems that it wants to change the user id and the sid by inserting a line instead of updating it. BUT in flag.module there is no db_insert('flagging').

We notice that when the first user is not anonymous, there is no problem, so it is linked to the replacement of uid when it is like 0.

Do you have any idea that could help ?

[Flag version : 7.x-3.5 and Session API version : 7.x-1.0-rc1]