CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • CustomField delete API call failing- tries to CREATE TRIGGER with deleted field
Pages: [1]

Author Topic: CustomField delete API call failing- tries to CREATE TRIGGER with deleted field  (Read 622 times)

DaveFF

  • I post occasionally
  • **
  • Posts: 54
  • Karma: 5
  • Developer at Future First
    • Future First
  • CiviCRM version: 4.4.13
  • CMS version: Drupal 7.34
  • MySQL version: 5.5
  • PHP version: 5.3
CustomField delete API call failing- tries to CREATE TRIGGER with deleted field
May 16, 2014, 08:21:11 am
In the uninstaller for the extension I'm developing, I use the API to delete the custom fields in the extension's custom group, and then delete the custom group itself. It appears that the first field is being deleted, but on the next one the API call returns error and dumps a backtrace. It is trying to recreate a trigger, and involving the now-deleted field when it does so. The database is left in an inconsistent state; the field's column is deleted from the custom field group's table, but the field's row remains in the civicrm_custom_field table. I'm wondering if this is a bug.

My function that does the API call:
Code: [Select]
  private static function deletefieldbyid($id) {
    $result = civicrm_api('CustomField', 'delete', array(
      'version'    => 3,
      'sequential' => 1,
      'id'         => $id,
    ));
    watchdog('Do_Not_Contact_Until', 'CustomField delete result: ' . print_r($result, TRUE), array(), WATCHDOG_DEBUG);
    return !civicrm_error($result);
  }

When the API call returned error on the field do_not_email_until, the watchdog looks like:
CustomField delete result: Array ( [error_code] => no such field [sql] => CREATE TRIGGER civicrm_value_do_not_contact_until_after_insert after insert ON civicrm_value_do_not_contact_until FOR EACH ROW BEGIN IF ( @civicrm_disable_logging IS NULL OR @civicrm_disable_logging = 0 ) THEN INSERT INTO log_civicrm_value_do_not_contact_until (id, entity_id, do_not_email_until, do_not_mail_until, do_not_sms_until, do_not_trade_until, log_conn_id, log_user_id, log_action) VALUES ( NEW.id, NEW.entity_id, NEW.do_not_email_until, NEW.do_not_mail_until, NEW.do_not_sms_until, NEW.do_not_trade_until, CONNECTION_ID(), @civicrm_user_id, 'insert');END IF; UPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.entity_id; END [nativecode=1054 ** Unknown column 'do_not_email_until' in 'NEW'] [tip] => add debug=1 to your API call to have more info about the error [is_error] => 1 [error_message] => DB Error: no such field )

And the backtrace:
May 16 15:43:02  [info] $backTrace = #0 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/Error.php(770): CRM_Core_Error::backtrace("backTrace", TRUE)
#1 [internal function](): CRM_Core_Error::exceptionHandler(Object(DB_Error))
#2 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/packages/PEAR.php(931): call_user_func((Array:2), Object(DB_Error))
#3 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/packages/DB.php(969): PEAR_Error->PEAR_Error("DB Error: no such field", -19, 16, (Array:2), "CREATE TRIGGER civicrm_value_do_not_contact_until_after_insert after insert O...")
#4 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/packages/PEAR.php(564): DB_Error->DB_Error(-19, 16, (Array:2), "CREATE TRIGGER civicrm_value_do_not_contact_until_after_insert after insert O...")
#5 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/packages/DB/common.php(1905): PEAR->raiseError(NULL, -19, NULL, NULL, "CREATE TRIGGER civicrm_value_do_not_contact_until_after_insert after insert O...", "DB_Error", TRUE)
#6 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/packages/DB/mysql.php(898): DB_common->raiseError(-19, NULL, NULL, NULL, "1054 ** Unknown column 'do_not_email_until' in 'NEW'")
#7 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/packages/DB/mysql.php(327): DB_mysql->mysqlRaiseError()
#8 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/packages/DB/common.php(1216): DB_mysql->simpleQuery("CREATE TRIGGER civicrm_value_do_not_contact_until_after_insert after insert O...")
#9 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/packages/DB/DataObject.php(2421): DB_common->query("CREATE TRIGGER civicrm_value_do_not_contact_until_after_insert after insert O...")
#10 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/packages/DB/DataObject.php(1613): DB_DataObject->_query("CREATE TRIGGER civicrm_value_do_not_contact_until_after_insert after insert O...")
#11 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/DAO.php(160): DB_DataObject->query("CREATE TRIGGER civicrm_value_do_not_contact_until_after_insert after insert O...")
#12 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/DAO.php(907): CRM_Core_DAO->query("CREATE TRIGGER civicrm_value_do_not_contact_until_after_insert after insert O...", FALSE)
#13 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/DAO.php(1693): CRM_Core_DAO::executeQuery("CREATE TRIGGER civicrm_value_do_not_contact_until_after_insert after insert O...", (Array:0), TRUE, NULL, FALSE, FALSE)
#14 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/DAO.php(1555): CRM_Core_DAO::createTriggers((Array:7))
#15 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/BAO/SchemaHandler.php(306): CRM_Core_DAO::triggerRebuild("civicrm_value_do_not_contact_until")
#16 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/BAO/CustomField.php(1870): CRM_Core_BAO_SchemaHandler::alterFieldSQL((Array:7), FALSE, TRUE)
#17 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/BAO/CustomField.php(1114): CRM_Core_BAO_CustomField::createField(Object(CRM_Core_BAO_CustomField), "delete")
#18 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/api/v3/CustomField.php(124): CRM_Core_BAO_CustomField::deleteField(Object(CRM_Core_BAO_CustomField))
#19 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/api/api.php(87): civicrm_api3_custom_field_delete((Array:3))
#20 /var/www/html/prod/drupal-7.27/sites/default/extensions/uk.org.futurefirst.networks.donotcontactuntil/CRM/Donotcontactuntil/Upgrader.php(41): civicrm_api("CustomField", "delete", (Array:3))
#21 /var/www/html/prod/drupal-7.27/sites/default/extensions/uk.org.futurefirst.networks.donotcontactuntil/CRM/Donotcontactuntil/Upgrader.php(83): CRM_Donotcontactuntil_Upgrader::deletefieldbyid("228")
#22 /var/www/html/prod/drupal-7.27/sites/default/extensions/uk.org.futurefirst.networks.donotcontactuntil/CRM/Donotcontactuntil/Upgrader/Base.php(264): CRM_Donotcontactuntil_Upgrader->uninstall()
#23 /var/www/html/prod/drupal-7.27/sites/default/extensions/uk.org.futurefirst.networks.donotcontactuntil/donotcontactuntil.civix.php(62): CRM_Donotcontactuntil_Upgrader_Base->onUninstall()
#24 /var/www/html/prod/drupal-7.27/sites/default/extensions/uk.org.futurefirst.networks.donotcontactuntil/donotcontactuntil.php(40): _donotcontactuntil_civix_civicrm_uninstall()
#25 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Extension/Manager/Module.php(64): donotcontactuntil_civicrm_uninstall()
#26 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Extension/Manager/Module.php(69): CRM_Extension_Manager_Module->callHook(Object(CRM_Extension_Info), "uninstall")
#27 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Extension/Manager.php(334): CRM_Extension_Manager_Module->onPreUninstall(Object(CRM_Extension_Info))
#28 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Admin/Form/Extensions.php(179): CRM_Extension_Manager->uninstall((Array:1))
#29 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/Form.php(261): CRM_Admin_Form_Extensions->postProcess()
#30 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/StateMachine.php(160): CRM_Core_Form->mainProcess()
#31 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/QuickForm/Action/Next.php(60): CRM_Core_StateMachine->perform(Object(CRM_Admin_Form_Extensions), "next", "Next")
#32 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/packages/HTML/QuickForm/Controller.php(203): CRM_Core_QuickForm_Action_Next->perform(Object(CRM_Admin_Form_Extensions), "next")
#33 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/packages/HTML/QuickForm/Page.php(103): HTML_QuickForm_Controller->handle(Object(CRM_Admin_Form_Extensions), "next")
#34 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/Controller.php(345): HTML_QuickForm_Page->handle("next")
#35 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/Page/Basic.php(381): CRM_Core_Controller->run()
#36 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/Page/Basic.php(178): CRM_Core_Page_Basic->edit(8, NULL)
#37 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Admin/Page/Extensions.php(127): CRM_Core_Page_Basic->run()
#38 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/Invoke.php(323): CRM_Admin_Page_Extensions->run((Array:3), NULL)
#39 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/Invoke.php(72): CRM_Core_Invoke::runItem((Array:13))
#40 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/CRM/Core/Invoke.php(52): CRM_Core_Invoke::_invoke((Array:3))
#41 /var/www/html/prod/drupal-7.27/sites/all/modules/civicrm/drupal/civicrm.module(456): CRM_Core_Invoke::invoke((Array:3))
#42 [internal function](): civicrm_invoke("admin", "extensions")
#43 /var/www/html/prod/drupal-7.27/includes/menu.inc(517): call_user_func_array("civicrm_invoke", (Array:2))
#44 /var/www/html/prod/drupal-7.27/index.php(21): menu_execute_active_handler()
#45 {main}


(Incidentally, I've also noticed that when I delete the custom group, its log_ table is not deleted. This causes problems when I reinstall my extension, so I have had to add a DROP TABLE IF EXISTS to the end of my uninstaller.)
Do Not Contact Until extension: https://civicrm.org/extensions/do-not-contact-until
Organisation Name De-duplicator extension: https://civicrm.org/extensions/organisation-name-de-duplicator

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • CustomField delete API call failing- tries to CREATE TRIGGER with deleted field

This forum was archived on 2017-11-26.