What do master and slave objects mean?


Question:

The documentation mentions setting master=false for child objects to select only those related to previously selected parent objects.

  1. Can you explain how this works and whether multiple "master" objects can be defined?
  2. Can I specify multiple "master" objects ?

Answer:

Regarding the first question, master/slave modes operate as follows in your export.json:

  • By default, all sObjects are in master mode, executing the query as defined, including any WHERE or LIMIT clauses.
  • For a slave object, the plugin removes the LIMIT but keeps the WHERE clause, adding conditions to select only related child records.

Consider this configuration example:

{
    "objects": [
        {
            "query" : "SELECT Id FROM Account LIMIT 10",
            "externalId": "Name",
            "operation": "Upsert"
        },
         {
            "query" : "SELECT Id, AccountId FROM Contact WHERE FirstName = 'John' LIMIT 100",
            "externalId": "LastName",
            "operation": "Upsert",
            "master": false
        }
    ]
}

Queries executed:

  • Account:
    SELECT Id, Name FROM Account LIMIT 10
    
  • Contact (slave to Account):
    SELECT Id, LastName, AccountId, Account.Name FROM Contact WHERE (FirstName = 'John') AND (AccountId IN (previously selected Account IDs)) # LIMIT clause is removed
    


As for the second question, yes, you can define multiple master objects.

Each master object is queried independently, and their results aren't interdependent unless specifically configured to relate through a slave object.

Example with multiple masters:

{
    "objects": [
        {
            "query" : "SELECT Id, Language__c FROM Account LIMIT 10",
            "externalId": "Name",
            "operation": "Upsert"
        },
         {
            "query" : "SELECT Id, AccountId FROM Contact WHERE FirstName = 'John' LIMIT 100",
            "externalId": "LastName",
            "operation": "Upsert",
            "master": false
        },
        {
            "query" : "SELECT Id, Name FROM Language__c WHERE Region__c == 'EU' LIMIT 5",
            "externalId": "Code__c",
            "operation": "Readonly"
        }
    ]
}

Queries executed:

  • Language__c and Account as master objects process their limits independently.
SELECT Id, Language__c FROM Account LIMIT 1
  SELECT Id, Name, Language__c, Language__r.Code__c FROM Account LIMIT 10
  • Contact (slave to Account) fetches only those linked to the Accounts fetched.
  SELECT Id, LastName, AccountId, Account.Name FROM Contact WHERE (FirstName = 'John') AND (AccountId IN (previously selected Account IDs)) # LIMIT clause is removed
Notes:
  • Using multiple masters might result in fewer overlaps and potential "missing lookup records" issues if there isn't a clear overlap between master datasets.
  • Setting master=false helps automatically link related records, reducing missing records issues. Be cautious with WHERE clauses in slave objects as they initially limit data before fetching related records.
Last updated on 23rd Apr 2024