I am creating a front end to a bioinformatics platform and I have to tables Experiment
and Sample
. An Experiment
can consist of many individual samples, e.g. a one-to-many relationship.
Experiment table relation looks like:
(id, experiment_name, additional_columns)
Sample table relation looks like:
(id, sample_name, experiment_id, additional_columns)
Now, I want to create a table for Images
that would be associated with an S3 uri and some metadata about an image; however, the images can be associated with either the Experiment
OR the Sample
. I could do something like,
CREATE TABLE images (
id serial4 NOT NULL,
s3_image_uri text NULL,
FOREIGN KEY (experiment_id) REFERENCES experiment (id),
FOREIGN KEY (sample_id) REFERENCES sample (id),
CONSTRAINT images_pkey PRIMARY KEY (id),
CONSTRAINT images_image_url_key UNIQUE (image_url),
);
The downside of this design is that in the future I could add additional tables that could have images associated with them as well.
How can I generalize this design so that I don't have to add an FK for each table that has an image association?