Context
The current data model correctly uses Thing -> NMA_Chemistry_SampleInfo -> NMA_Radionuclides.
Problem
alembic/versions/545a5b77e5e8_add_chemistry_backfill_columns_to_.py upgrade drops NMA_Radionuclides.thing_id and its FK as if always present, while d9f1e2c3b4a5 already drops that column. This can fail in environments with different migration history/state.
Proposed fix
- Make 545a5b77e5e8 drop logic defensive (introspect columns/constraints before dropping).
- Keep downgrade restoration safe (add nullable, backfill from NMA_Chemistry_SampleInfo.thing_id, then enforce NOT NULL + FK).
- Add migration tests for both preconditions (thing_id present vs already absent).
Acceptance criteria
- alembic upgrade head succeeds in both preconditions.
- Downgrade of 545a5b77e5e8 succeeds with existing radionuclide rows.
- Runtime association model remains unchanged (NMA_Radionuclides linked via chemistry_sample_info_id).