-
Notifications
You must be signed in to change notification settings - Fork 1
Description
JIRA Ticket
Summary
The migration to Thing is currently breaking NM_Aquifer legacy primary/foreign key relationships.
The core issue is failure to persist legacy NM_Aquifer identifiers (GUIDs) as stable identities in Thing, which prevents downstream tables from maintaining correct foreign key relationships.
All models that become or reference a Thing must preserve their original NM_Aquifer primary keys and use those identifiers consistently during cascading transfers.
Background
Thing is acting as an identity unification layer for legacy NM_Aquifer entities (e.g., Location, Well, Site). However, current migrations treat Thing as a newly generated surrogate instead of a continuation of legacy identity.
As a result:
- Legacy PKs (e.g.,
LocationID,WellID) are not reliably persisted inThing - Downstream tables (e.g., WaterLevels, WeatherData) cannot reconstruct correct relationships
- Orphan prevention and FK enforcement fail because parent identity is ambiguous or lost
Issue 363 (adding thing_id FKs and cascade rules) is necessary but insufficient unless legacy identity is preserved first.
Core Requirements (Identity Preservation)
For all NM_Aquifer entities that map to Thing:
-
Persist Legacy Primary Keys
- Store NM_Aquifer GUIDs in
Thing(e.g.,legacy_id,source_pk, or equivalent) - One legacy GUID → exactly one
Thing - No regeneration or remapping of identity
- Store NM_Aquifer GUIDs in
-
Explicit Legacy → Thing Mapping
- Location → Thing must persist
LocationID - WellData → Thing (Ocotillo) must persist
WellID - Mapping must be deterministic and cacheable
- Location → Thing must persist
-
Foreign Key Continuity
- Cascading transfer scripts must:
- Resolve
thing_idvia legacy GUID lookup - Never infer relationships indirectly (e.g., via
PointIDonly)
- Resolve
- All child tables must reference the same
Thingcreated from the legacy parent
- Cascading transfer scripts must:
Downstream Enforcement (After Identity Is Correct)
Once identity continuity is guaranteed, enforce relational integrity:
1. Child Models
thing_idFK withNOT NULLondelete="CASCADE"- Python-level orphan prevention via
@validates
2. Thing Reverse Relationships
cascade="all, delete-orphan"passive_deletes=True
3. Backfills
- Build O(1) legacy GUID →
thing_idcache - Filter to valid Things
- Skip and log orphan legacy records (do not auto-create parents)
Acceptance Criteria
- All necessary NM_Aquifer legacy GUIDs are persisted in
Thing - No duplicate
Thingrows for a single legacy entity - Cascading transfers resolve parents via legacy GUIDs
- All Thing children have enforced
thing_idFKs - Orphan creation is impossible at DB and ORM levels
- Tests assert identity continuity across migrations