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) »
  • Support »
  • Upgrading CiviCRM (Moderator: Deepak Srivastava) »
  • DB Error: constraint violation civicrm_activity_assignment upgrading from 3.3.6
Pages: [1]

Author Topic: DB Error: constraint violation civicrm_activity_assignment upgrading from 3.3.6  (Read 7825 times)

mdlueck

  • Ask me questions
  • ****
  • Posts: 382
  • Karma: 4
  • CiviCRM version: 4.7.24
  • CMS version: Drupal 6.x
  • MySQL version: 5.5.54
  • PHP version: 5.3.10
DB Error: constraint violation civicrm_activity_assignment upgrading from 3.3.6
June 28, 2011, 05:48:25 pm
I am testing the upgrade on Drupal 6 from Civi 3.3.6 to 3.4.4 and ran into the following error:

Code: [Select]
Array
(
    [callback] => Array
        (
            [0] => CRM_Core_Error
            [1] => handle
        )

    [code] => -3
    [message] => DB Error: constraint violation
    [mode] => 16
    [debug_info] => INSERT INTO civicrm_activity_assignment ( activity_id, assignee_contact_id ) SELECT ca.id, ca.source_contact_id FROM civicrm_activity ca LEFT JOIN civicrm_activity_assignment cas ON ( cas.activity_id = ca.id ) WHERE ca.activity_type_id = @option_value_membership_reminder AND cas.id IS NULL [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (`d1cir_civicrm/civicrm_activity_assignment`, CONSTRAINT `FK_civicrm_activity_assignment_assignee_contact_id` FOREIGN KEY (`assignee_contact_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE C)]
    [type] => DB_Error
    [user_info] => INSERT INTO civicrm_activity_assignment ( activity_id, assignee_contact_id ) SELECT ca.id, ca.source_contact_id FROM civicrm_activity ca LEFT JOIN civicrm_activity_assignment cas ON ( cas.activity_id = ca.id ) WHERE ca.activity_type_id = @option_value_membership_reminder AND cas.id IS NULL [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (`d1cir_civicrm/civicrm_activity_assignment`, CONSTRAINT `FK_civicrm_activity_assignment_assignee_contact_id` FOREIGN KEY (`assignee_contact_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE C)]
    [to_string] => [db_error: message="DB Error: constraint violation" code=-3 mode=callback callback=CRM_Core_Error::handle prefix="" info="INSERT INTO civicrm_activity_assignment ( activity_id, assignee_contact_id ) SELECT ca.id, ca.source_contact_id FROM civicrm_activity ca LEFT JOIN civicrm_activity_assignment cas ON ( cas.activity_id = ca.id ) WHERE ca.activity_type_id = @option_value_membership_reminder AND cas.id IS NULL [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (`d1cir_civicrm/civicrm_activity_assignment`, CONSTRAINT `FK_civicrm_activity_assignment_assignee_contact_id` FOREIGN KEY (`assignee_contact_id`) REFERENCES `civicrm_contact` (`id`) ON DELETE C)]"]
)

Suggestions please?

Fortunately this is on a test copy of our 3.3.6 production site. "No pressure!"  :P
--
Michael Lueck
Lueck Data Systems
http://www.lueckdatasystems.com/

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: DB Error: constraint violation civicrm_activity_assignment upgrading from 3.3.6
June 29, 2011, 12:27:53 am
Just a wild guess, your civicrm_activity table has some activities for deleted contacts. So try deleting them first and retry upgrade.

Quote
SELECT source_contact_id FROM civicrm_activity WHERE source_contact_id NOT IN ( select id from civicrm_contact );

Above sql should give you orphan activities.

HTh
Kurund
Found this reply helpful? Support CiviCRM

mdlueck

  • Ask me questions
  • ****
  • Posts: 382
  • Karma: 4
  • CiviCRM version: 4.7.24
  • CMS version: Drupal 6.x
  • MySQL version: 5.5.54
  • PHP version: 5.3.10
Re: DB Error: constraint violation civicrm_activity_assignment upgrading from 3.3.6
June 29, 2011, 04:13:29 am
Thanks for the suggestion. Here is the database's reply:

Code: [Select]
MySQL returned an empty result set (i.e. zero rows). (Query took 0.0347 sec)

SQL query:
SELECT source_contact_id
FROM civicrm_activity
WHERE source_contact_id NOT
IN (
  SELECT id
  FROM civicrm_contact
)
LIMIT 0 , 30;
--
Michael Lueck
Lueck Data Systems
http://www.lueckdatasystems.com/

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: DB Error: constraint violation civicrm_activity_assignment upgrading from 3.3.6
June 29, 2011, 02:18:29 pm
Re-formatted the query & to make it easier to look at
Code: [Select]
INSERT INTO civicrm_activity_assignment ( activity_id, assignee_contact_id )

SELECT ca.id, ca.source_contact_id
FROM civicrm_activity ca
LEFT JOIN civicrm_activity_assignment cas ON ( cas.activity_id = ca.id ) 

WHERE ca.activity_type_id = @option_value_membership_reminder AND cas.id IS NULL
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

mdlueck

  • Ask me questions
  • ****
  • Posts: 382
  • Karma: 4
  • CiviCRM version: 4.7.24
  • CMS version: Drupal 6.x
  • MySQL version: 5.5.54
  • PHP version: 5.3.10
Re: DB Error: constraint violation civicrm_activity_assignment upgrading from 3.3.6
June 29, 2011, 03:05:30 pm
A big thanks to bgm and eileen via IRC, this problem is resolved.

Turns out that some really REALLY old data was stuck in that table. We went production with Civi earlier this year, so I purged from the table all records older than this year... AFTER dropping all tables and restoring from backup of course.

So, steps to success:
1) Drop all tables until all tables have been removed
2) Restore from backup
3) SELECT * FROM `civicrm_activity` WHERE `activity_date_time` < '2011-01-01 00:00:00'
4) DELETE FROM `civicrm_activity` WHERE `activity_date_time` < '2011-01-01 00:00:00'
5) Once more to make sure 0 records are returned: SELECT * FROM `civicrm_activity` WHERE `activity_date_time` < '2011-01-01 00:00:00'
6) On with the upgrade to a green light success. Woo hoo, on with 3.4.4 testing!

Thanks again bgm and eileen!!!  ;D
--
Michael Lueck
Lueck Data Systems
http://www.lueckdatasystems.com/

chriscant

  • I post occasionally
  • **
  • Posts: 66
  • Karma: 4
    • PHDCC
  • CiviCRM version: 4.2.19
Re: DB Error: constraint violation civicrm_activity_assignment upgrading from 3.3.6
November 24, 2011, 09:10:18 am
I found this problem as well.  I found the value of @option_value_membership_reminder to be 27 in my case:

SELECT @option_value_membership_reminder := value FROM civicrm_option_value v, civicrm_option_group g WHERE v.option_group_id = g.id AND g.name = 'activity_type' AND v.name = 'Membership Renewal Reminder';

The insert problem was due to ca.source_contact_id being NULL.  I found these cases as follows, using the value 27 found above:

SELECT ca.id, ca.source_contact_id
FROM civicrm_activity ca
   LEFT JOIN civicrm_activity_assignment cas
   ON ( cas.activity_id = ca.id )
WHERE ca.activity_type_id = 27 AND cas.id IS NULL AND ca.source_contact_id IS NULL

In my case there were three such records.  I deleted them and the script ran OK.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: DB Error: constraint violation civicrm_activity_assignment upgrading from 3.3.6
November 24, 2011, 09:43:00 am

hey chris:

can u please file an issue with a link to this forum post. We should fix it to avoid problems for folks who will upgrade later :)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

chriscant

  • I post occasionally
  • **
  • Posts: 66
  • Karma: 4
    • PHDCC
  • CiviCRM version: 4.2.19
Re: DB Error: constraint violation civicrm_activity_assignment upgrading from 3.3.6
November 29, 2011, 11:18:45 am
Reported here: http://issues.civicrm.org/jira/browse/CRM-9245

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Upgrading CiviCRM (Moderator: Deepak Srivastava) »
  • DB Error: constraint violation civicrm_activity_assignment upgrading from 3.3.6

This forum was archived on 2017-11-26.