Short introduction – logic behind the data
I am working on a PostgreSQL database that stores experimental data (Raman spectra) and their metadata. It is necessary, in addition to the "normal" spectra of our samples, to acquire and save special spectra for the instrument calibration purposes.
Each time I do an experiment I create a new record in the table measurements
, where I specify the measurement type
and the corresponding metadata (foreign keys to other tables).
The issue is, that the metadata type differs depending on the measurement type
:
- Sample measurement – we have to store
sample_id
from the tablesamples
- Standard substances – in this case we specify a substance from the table
substances
- Special standardized light source –
source_id
has to be provided - Dark frames – simple, no data from other tables are necessary
samples, substances and light sources have completely different properties, so we have to keep them in separate tables, linked to the table measurements
via foreign keys.
I think my data have a polymorphic relation here (correct me if I'm wrong).
The problem
In the current design, the value stored in column measurementtype_id
(highlighted red) defines which foreign keys (highlighted yellow) have to be NULL
ed and which have to contain a value.
I am pretty sure that this design violates the third normal form. We can have a scenario when metadata of a wrong type is associated with the measurement. How can I ensure the data integrity? Unfortunately, I was not able to find a reasonable solution so far...