Skip to content

WellData and Location (anything that becomes a Thing) children should have enforced FK associations with orphan prevention #363

@kbighorse

Description

@kbighorse

Summary

Audit all models that are logical children of Thing and ensure they have properly enforced FK relationships with orphan prevention, following the pattern established in ChemistrySampleInfo.

Background

We recently added thing_id FK to ChemistrySampleInfo with:

  • NOT NULL constraint (database-level)
  • @validates decorator (Python-level orphan prevention)
  • ORM relationship() with cascade="all, delete-orphan"
  • Backfill filtering to prevent orphan records

This pattern should be applied consistently to all Thing children.

Models to Review

Audit these models in db/nma_legacy.py and elsewhere for Thing association:

Model Current State Action Needed
ChemistrySampleInfo ✅ Has thing_id FK Done
WeatherData ❌ No FK (has PointID) See #362
Other NMA legacy tables ? Audit needed

Required Pattern

Each Thing child should have:

1. Model Definition

class ChildModel(Base):
    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    thing_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("thing.id", ondelete="CASCADE"), nullable=False
    )
    
    thing: Mapped["Thing"] = relationship("Thing", back_populates="child_models")
    
    @validates("thing_id")
    def validate_thing_id(self, key, value):
        if value is None:
            raise ValueError("ChildModel requires a parent Thing")
        return value

2. Thing Reverse Relationship

# In db/thing.py
child_models: Mapped[List["ChildModel"]] = relationship(
    "ChildModel",
    back_populates="thing",
    cascade="all, delete-orphan",
    passive_deletes=True,
)

3. Backfill Updates

  • Build Thing ID cache for O(1) lookups
  • Filter to valid Things before insert
  • Skip/warn on orphan records

4. Tests (TDD)

  • Test relationship navigation (Thing → Children)
  • Test cascade delete behavior
  • Test orphan prevention (expect ValueError)

Acceptance Criteria

  • Audit all NMA legacy models for Thing relationship
  • Document which models need updates
  • Each child model has thing_id FK (NOT NULL)
  • Each child model has @validates orphan prevention
  • Thing has reverse relationships for all children
  • Backfills filter to valid Things
  • Unit tests cover all relationships

Reference Implementation

  • Model: db/nma_legacy.py - ChemistrySampleInfo
  • Backfill: transfers/backfill/chemistry_sampleinfo.py
  • Tests: tests/test_nma_chemistry_lineage.py

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions