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 »
  • Using CiviCRM (Moderator: Dave Greenberg) »
  • contacts with no primary location
Pages: [1]

Author Topic: contacts with no primary location  (Read 2002 times)

dalin

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 8
  • CiviCRM version: many
  • CMS version: Drupal 6
  • MySQL version: 5.0
  • PHP version: 5.2
contacts with no primary location
July 10, 2007, 11:08:51 am
I'm dealing with a problem where out of 50k contacts in civicrm, there are 413 that have locations, but none of these locations are marked as primary.  These can be listed with the following query:

Code: [Select]
SELECT *
FROM (
  SELECT DISTINCT entity_id
  FROM civicrm_location
  WHERE is_primary = 0) AS np
LEFT JOIN (
  SELECT DISTINCT entity_id
  FROM civicrm_location
  WHERE is_primary = 1) AS p
on np.entity_id = p.entity_id
WHERE p.entity_id IS NULL;

This causes problems when you attempt to edit the drupal record for the contact.  civicrm will attempt to update the primary email for the primary address for the contact, if there's no primary address it fails with the following error:

Code: [Select]
Sorry. A non-recoverable error has occurred.

    Database Error Code: Cannot add or update a child row: a foreign key
constraint fails, 1216

    Return to CiviCRM menu.

Error Details:

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

    [code_] => -3
    [message] => DB Error: constraint violation
    [mode] => 16
    [debug_info] => INSERT INTO civicrm_email (email , is_primary ) VALUES
('multimedia@bioneers.org' ,  1 )  [nativecode=1216 ** Cannot add or update
a child row: a foreign key constraint fails]
    [type] => db_error
    [user_info] => INSERT INTO civicrm_email (email , is_primary ) VALUES
('multimedia@bioneers.org' ,  1 )  [nativecode=1216 ** Cannot add or update
a child row: a foreign key constraint fails]
    [to_string] => [db_error: message="DB Error: constraint violation"
code=-3 mode=callback callback=CRM_Core_Error::handle prefix="" info="INSERT
INTO civicrm_email (email , is_primary ) VALUES ('multimedia@bioneers.org' ,
1 )  [nativecode=1216 ** Cannot add or update a child row: a foreign key
constraint fails]"]
)

Has anyone else experienced this?
--
Dave Hansen-Lange
Web Developer
Advomatic LLC
http://advomatic.com
Hong Kong office

dalin

  • I post occasionally
  • **
  • Posts: 89
  • Karma: 8
  • CiviCRM version: many
  • CMS version: Drupal 6
  • MySQL version: 5.0
  • PHP version: 5.2
Re: contacts with no primary location
July 10, 2007, 11:29:34 am
You can fix this situation with the following query.  It can probably be optimized, but you only run it once so it's not a big deal that it takes 13secs.

Code: [Select]
UPDATE civicrm_location lu, (
  SELECT MIN(l.id) AS id, l.entity_id
  FROM civicrm_location l
  INNER JOIN (
    SELECT np.entity_id
    FROM
      (SELECT DISTINCT entity_id
      FROM civicrm_location
      WHERE is_primary = 0) AS np
    LEFT JOIN
      (SELECT DISTINCT entity_id
      FROM civicrm_location
      WHERE is_primary = 1) AS p
    ON np.entity_id = p.entity_id
    WHERE p.entity_id IS NULL
  ) AS nopri
  ON l.entity_id = nopri.entity_id
  GROUP BY l.entity_id
) as eids
SET lu.is_primary = 1
WHERE lu.id = eids.id;
--
Dave Hansen-Lange
Web Developer
Advomatic LLC
http://advomatic.com
Hong Kong office

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM (Moderator: Dave Greenberg) »
  • contacts with no primary location

This forum was archived on 2017-11-26.