Table of Contents Previous Next

Sugar Developer Guide

Version 5.1


Chapter 2 Application Framework : Databases : Primary Keys, Foreign Keys, and GUIDs

Primary Keys, Foreign Keys, and GUIDs
By default, Sugar uses globally unique identification values (GUIDs) for primary keys for all database records. Sugar provides a create_guid() utility function for creating these GUIDs in the following format: aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee. The primary key column length is 36 characters.
The GUID format and value has no special meaning in Sugar other than the ability to match records in the DB. Sugar links two records (such as an Account record with a Contact record) with a specified ID in the record type relationship table (e.g. accounts_contacts).
Sugar allows a primary key to contain any unique string. This could either be a different GUID algorithm, a key that has some meaning (such as bean type first, followed by info), an external key, and/or auto-incrementing numbers (converted to strings). The Sugar development team chose GUIDs rather than auto-incrementing keys to allow for easier data synchronization across databases (in order to avoid primary key collisions). This data synchronization issue comes into play when the Sugar Offline Client (part of Sugar Enterprise) syncs data between the main Sugar installation and the Offline Client or when developers use the Sugar SOAP APIs or a tool like Talend for data synchronization.
The strategy of the Offline Client using GUIDs for primary keys is elegant in that it is very easy to implement and handling data conflicts is simpler than with other schemes. If a developer changes Sugar to use some other ID scheme and does need to accommodate data synchronization across data stores, then he would have to either partition the IDs ahead of time or work out a system similar to the Sugar implementation for Cases, Quotes and Bugs. For these modules, which have human-readable ID numbers (integers) that need to be synchronized across databases, Sugar implements a server ID that is globally unique and concatenates that with an incrementing Case, Quotes or Bug number. Attempting such a change to Sugar, while certainly possible, would require some careful planning and implementation.
However if the developer does not need to worry about data synchronization issues, then he can certainly change the primary key format to some other unique string.
Also, it is not a problem to import data from a previous system with one primary key format and then simply have all new records in Sugar use the GUID primary key format. All keys simply need to be stored as unique strings with no more than 36 characters.
To implement a new primary key method or to import existing data with a different primary key format and then rely on the existing GUID mechanism for new records, there are a few things to look out for:

Table of Contents Previous Next

Copyright 2004-2008 SugarCRM Inc.
Product License