I get the following error message when I try to set a webform conditional.

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '20660-0' for key 'PRIMARY': INSERT INTO {webform_conditional} (nid, rgid, weight) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 20660 [:db_insert_placeholder_1] => 0 [:db_insert_placeholder_2] => -1 ) in drupal_write_record() (line 7334 of /home/whistler/public_html/dev/includes/common.inc).

I have tried setting different fields as the conditional trigger but get the same effect. My webform was recently upgraded from 3.x branch to 4.12 so I thought that might be the problem but I completely uninstalled webform and removed all db tables and did re-install of the module and created a new form from scratch but still get the error when trying to set a conditional

This site was upgraded from drupal 6 to drupal 7 about 5 months ago.

Can anyone point to in the right direction for a resolution?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jacqueschoquette created an issue. See original summary.

guilopes’s picture

Same issue here, the following patch should solve this issue

I had this issue when exported the webform by features and tried re import it

guilopes’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: webform-issues_when_import_conditionals-2690815-1-7.patch, failed testing.

guilopes’s picture

guilopes’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 5: webform-issues_when_import_conditionals-2690815-1-7.patch, failed testing.

guilopes’s picture

guilopes’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 8: webform-issues_when_import_conditionals-2690815-1-7_0.patch, failed testing.

guilopes’s picture

guilopes’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 11: webform-issues_when_import_conditionals-2690815-1-7.patch, failed testing.

DanChadwick’s picture

Status: Needs review » Needs work
Issue tags: -webform, -conditional

In debugging this, I see a bigger problem. The intention of this code to is allow the conditionals to be edited, but due to the way form values are converted into the webform conditionals, the conditionals array and individual conditionals will NEVER be identical when using the conditionals-definition page.

So we can push forward and make the form values into an actual conditional (without superfluous form_state values, such as button names and weights, and with the missing nid). Or we can retreat and simply delete and re-insert the conditionals when using the conditionals page.

DanChadwick’s picture

Worked on this a bit more. It turns out to be very hard to make the condtionals from the form exactly match the database to allow the array comparisons for delete/insert to work. There are missing fields which are supplied by the database (e.g. andor) and fields which are set during the process of the insert (the rule/action's nid/rgid/rid/aid).

I'm posting the code I used to remove the elements in the form_state that aren't in the schema, but it isn't sufficient:

/**
 * Helper. Remove non-database fields from conditionals.
 *
 * Allows edited conditionals to be compared with the conditionals in the
 * database to determine which need to be deleted and/or inserted.
 */
function webform_condiionals_form_clean($conditionals) {
  foreach (array('', 'rules', 'actions') as $suffix) {
    $raw_schema = drupal_get_schema(trim("webform_conditional_{$suffix}", '_'));
    $schema[$suffix] = $raw_schema['fields'];
  }
  foreach ($conditionals as &$conditional) {
    foreach ($conditional as $field_name => &$field_data) {
      if (isset($schema[$field_name])) {
        foreach ($field_data as &$rule_or_action) {
          foreach ($rule_or_action as $subfield_name => $subfield_data) {
            if (!isset($schema[$field_name][$subfield_name])) {
              unset($rule_or_action[$subfield_name]);
            }
          }
        }
      }
      else {
        if (!isset($schema[''][$field_name])) {
          unset($conditional[$field_name]);
        }
      }
    }
  }
  unset($conditional, $field_data, $rule_or_action); // Drop PHP references.
  return $conditionals;
}

I think the best course of action is to fix the delete/insert, understanding that for the case of editing the conditionals with the form, every conditional will be deleted and then inserted. The differential optimization code will probably work for features though.

  • DanChadwick committed 78277fd on 7.x-4.x
    Issue #2690815 by guilopes: Fixed integrity constraint violation when...
DanChadwick’s picture

Status: Needs work » Fixed
FileSize
1.64 KB

Committed tweaked patch to 7.x-4.x. Thanks for your help everyone.

DanChadwick’s picture

Version: 7.x-4.12 » 8.x-4.x-dev
Category: Bug report » Task
Status: Fixed » Patch (to be ported)
fenstrat’s picture

Version: 8.x-4.x-dev » 7.x-4.x-dev
Category: Task » Bug report
Status: Patch (to be ported) » Fixed

Closing to clear out the old Webform 8.x-4.x branch. See #2827845: [roadmap] YAML Form 8.x-1.x to Webform 8.x-5.x.

  • DanChadwick committed 78277fd on 8.x-5.x
    Issue #2690815 by guilopes: Fixed integrity constraint violation when...
guilopes’s picture

Status: Fixed » Closed (fixed)

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