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) »
  • location API is throwing DB constraint violation error
Pages: [1]

Author Topic: location API is throwing DB constraint violation error  (Read 771 times)

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
location API is throwing DB constraint violation error
February 14, 2011, 08:35:14 pm
In my code, I am looking up the household address, then trying to assign it as a "shared with" address for the individual who is a household member.


My code snippet:

$tmp_address2 = array( 1 => array( 'location_type_id'       => 1,
                                      'is_primary'             => 1,
                                      'master_id'          => $hhid,    
                                      'city'                   => $tmp_address[city],
                                      'state_province'         => $tmp_address[state_province_id],
                                      'street_address'         => $tmp_address[street_address],
                                      'supplemental_address_1' => $tmp_address[supplemental_address_1],
                                      'postal_code'          => $tmp_address[postal_code] ,
                                      'country_id'             => $tmp_address[country_id] ,
                                      'geo_code_1'      => $tmp_address[geo_code_1],
                                      'geo_code_2'      => $tmp_address[geo_code_2]                                   
                                       )
                        );
        // Put them in an array and then add to $params array
        $loc_params = array('version'    => '3.0',
                        'contact_id' => $cid,
                        'address'    => $tmp_address2
                        );
       
        echo "<br><br>loc_params to send to API: <br>";
        print_r($loc_params);
       
       $newLocation =& civicrm_location_add($loc_params);


At runtime I see:
loc_params to send to API:
Array ( [version] => 3.0 [contact_id] => 7226 [address] => Array ( [1] => Array ( [location_type_id] => 1 [is_primary] => 1 [master_id] => 6380 [city] => Chicago [state_province] => 1012 [street_address] => 4342 S. Trumbull [supplemental_address_1] => [postal_code] => 60632 [country_id] => 1228 [geo_code_1] => 41.814075 [geo_code_2] => -87.71056 ) ) )

Yet the error message is:
DB Error: constraint violation

[debug_info] =&gt; UPDATE  civicrm_address  SET contact_id = 7226 , location_type_id = 1 , is_primary = 1 , is_billing = 0 , street_address = '4342 S. Trumbull' , city = 'Chicago' , state_province_id = 1012 , postal_code = '60632' , country_id = 1228 , geo_code_1 = 41.8140750 , geo_code_2 = -87.7105600 , master_id = 6380   WHERE (  civicrm_address.id = 3670 )   [nativecode=1452 ** Cannot add or update a child row: a foreign key constraint fails (`hydepark_main`.`civicrm_address`, CONSTRAINT `FK_civicrm_address_master_id` FOREIGN KEY (`master_id`) REFERENCES `civicrm_address` (`id`) ON DELETE SET NULL)]
    [type] =&gt; DB_Error
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: location API is throwing DB constraint violation error
February 14, 2011, 10:33:27 pm
It implies that

[master_id] => 6380

is not a valid address ID - can you confirm it is?
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

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: location API is throwing DB constraint violation error
February 15, 2011, 07:57:04 am
I was incorrectly passing the contact id of the household. I fixed my code to send the id of the location instead, and now its working for the contacts that I had previously imported.

However, it is throwing a different error for contacts that were created in the user interface.

The code is:
 echo "<br><br>loc_params to send to API: <br>";
  print_r($loc_params);
 $newLocation =& civicrm_location_update($loc_params);
               
  $tmp_newloc = civicrm_error( $newLocation );   
 if( $tmp_newloc  ) {
            echo '<br>Error occured while creating Location: '.$tmp_newloc ;
        } else {
           
             echo '<br>Location Created:';
             print_r( $newLocation );
             echo "<br>";
        }

--------------------

The results on the screen are:


loc_params to send to API:
Array ( [version] => 3.0 [contact_id] => 8291 [address] => Array ( [1] => Array ( [location_type_id] => 1 [is_primary] => 1 [master_id] => 3640 [city] => Chicago [state_province] => 1012 [street_address] => 123 Main Ave. [supplemental_address_1] => [postal_code] => 60637 [country_id] => 1228 [geo_code_1] => 41.791758 [geo_code_2] => -87.590584 ) ) )

Error occured while creating Location: 1
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • location API is throwing DB constraint violation error

This forum was archived on 2017-11-26.