Sub topic 2
External IDs: identifying records from another system
This chapter assumes you know basic SQL (INSERT, UPDATE, SELECT, primary keys) and that data is often synchronized between separate systems. The next chapters cover storing and enforcing the ID. This chapter covers what an external ID is and how to choose one.
What an external ID is
An external ID is the identifier that another system uses for a record. If your CRM calls a customer C-1001, then C-1001 is that customer's external ID from your point of view. Your database did not create it and does not control it.
An upsert needs a match key. When a record arrives, the load asks whether a row for this record already exists. If yes, it updates the row. If no, it inserts one. The external ID is what answers that question.
What makes a good external ID
A good external ID has these properties:
- Stable. The source never changes it for the same real-world record.
- Unique within its source. No two records in that system share it.
- Never reused. If a record is deleted, the source does not hand its ID to a new record.
- Always present. Every record you load has one.
The source system's own primary key usually meets all four. Prefer it over anything you derive yourself.
Fields that look like IDs but are not
Some fields look unique but break the stability rule. Avoid them as match keys.
| Candidate | Problem |
|---|---|
| Email address | People change addresses, and shared mailboxes appear on several records. |
| Name | Names repeat and get corrected. |
| Row number in an export | It changes whenever the export is sorted or filtered differently. |
| Timestamp | Two records can share one, and the value changes on edit. |
If you match on a value that can change, the next load will not find the old row. It will insert a second row for the same customer.
Scope the ID by its source system
An external ID is only unique inside the system that issued it. The CRM might use 1001 and the billing system might also use 1001 for a different customer. Treat the identity of a record as a pair: the source system name plus the external ID.
from typing import NamedTuple
class RecordKey(NamedTuple):
source_system: str
external_id: str
crm = RecordKey("crm", "1001")
billing = RecordKey("billing", "1001")
print(crm == billing) # False: same ID value, different records
print(crm == RecordKey("crm", "1001")) # True: same recordEven if you load from only one system today, storing the source name costs one column. It avoids a painful migration when a second source arrives.
Keep the ID as text
Store external IDs as text, even when they look numeric. A numeric column drops leading zeros (00042 becomes 42) and fails on sources that later introduce IDs like A-17. You also never do arithmetic on an ID, so a number type gives you nothing.
Treat the value as opaque. Compare it exactly as received, apart from the cleanup rules covered in the chapter on normalizing external IDs.
Common mistakes
- Using your own internal key as the match key for incoming data. The source system has never heard of it, so it cannot send it.
- Assuming IDs are unique across systems. Include the source system in the key, as shown above.
- Trusting IDs to be clean. The same ID can arrive as
c-1001in one file andC-1001(with a trailing space) in another. Decide on one form before matching. - Skipping records with a missing ID. Silently dropping them hides data loss. Reject them loudly or send them to a review list.
Exercise
A source system exports customers with an email column and an account_no column. The source documentation says account numbers are assigned once and are never reused. Which should you use as the external ID, and what else should you store with it?
Show answer
Use account_no, stored as text. It is stable and never reused, while emails can change or be shared. Store the source system name alongside it, so that the pair (source system, account number) identifies the record.