many-to-many tables not supported by dm_examine_constraints
#1367
Replies: 4 comments 7 replies
-
Thanks. A "many-to-many" relationship is typically modeled with an intermediate table that has two foreign keys. Can you please describe the relationships in your data? Does one library(conflicted)
library(dm)
sample <- tibble(
sample_id = c("sample1", "sample2", "sample3"),
property = c("A", "B", "C")
)
sample_set <- tibble(
sample_set_id = c("set1", "set1", "set2", "set2"),
sample_id = c("sample1", "sample2", "sample2", "sample3")
)
dataset <- tibble(
dataset_id = c("dataset1", "dataset2"),
sample_set_id = c("set1", "set2")
)
model <- as_dm(list(
sample = sample,
sample_set = sample_set,
dataset = dataset
))
model <- dm_add_fk(model,
table = "sample_set",
columns = "sample_id",
ref_table = "sample",
ref_columns = "sample_id"
)
model <- dm_add_fk(model,
table = "sample_set",
columns = "sample_set_id",
ref_table = "dataset",
ref_columns = "sample_set_id"
)
model %>% dm_draw() model %>% dm_examine_constraints()
#> ℹ All constraints satisfied. Created on 2022-08-06 by the reprex package (v2.0.1) |
Beta Was this translation helpful? Give feedback.
-
Can you have multiple sample sets per dataset, and multiple datasets per sample set? What are the semantics of |
Beta Was this translation helpful? Give feedback.
-
In your data I now see a third dataset that is related to the first sample set. Is the third dataset also related to the second sample? |
Beta Was this translation helpful? Give feedback.
-
To accurately model this, we will need a fourth and perhaps a fifth table. The only remaining question I have is where to put it and how to wire it. This is not really a restriction of the dm package, most databases will enforce the same. |
Beta Was this translation helpful? Give feedback.
-
I have a data model that includes a many-to-many table, which I was able to validate with earlier versions of the dm package. Now, using
dm_examine_constraints
on the same model produces a validation error. It appears that the function is defining a key type "unique key", even when I have not set a primary key in the model. The documentation says that the output ofdm_examine_constraints
includes a vectorkind
with values"PK" or "FK"
, but it actually creates additional keys with kind"UK"
not set by the user.Here is a reproducible example:
Beta Was this translation helpful? Give feedback.
All reactions