There are a number of questions related to parent/child and many-to-many relationships, but I couldn't quite find one that seemed to make sense for my needs.
I have a database of "samples", let's just consider it some kind of goop in a bucket. Each sample has an associated ID. What I'd like to capture is that any given sample can have a parent (i.e. one sample can be split into multiple new samples by pouring the bucket into a bunch of smaller jars). Additionally, one sample could have multiple parent samples; create a new jar by combining multiple parent samples. The key here is that the child and parent samples represent the same thing (a sample) and should thus be members of the same table.
I started with a samples
table, where the columns are id
, date
, project
and parent
. The id
is an alphanumeric ID (i.e. A0001
, A0002
) and the parent
is an array of ID's that should refer back to the list of samples. In other words, the parent must be a member of samples
.
I wasn't able to set a foreign key relationship between id
and the list of id
s in parent
.
Is there a way to do this?
I thought about creating a parents
table, but then I might have something like:
samples
-------
id date project parent
A0001 2020/11/01 alpha []
A0002 2020/11/01 beta []
A0003 2020/11/05 gamma [A0001, A0002]
A0004 2020/11/05 gamma [A0002]
parents
-------
id children
A0001 [A0003]
A0002 [A0003, A0004]
I still have a situation where I need to use a foreign key relationship between a CHAR
and a CHAR[]
.
I would like to be able to query the information (such as date
and project
) via the id
s available in the array of parents. I think I could handle this outside the database, but it seems like a hack, and not the correct way to go.
For context, I'm using Postgresql v13.0 and Hasura GraphQL v1.3.3.
Any help or advice is appreciated.