dm_add_fk | R Documentation |
dm_add_fk()
marks the specified columns
as the foreign key of table table
with
respect to a key of table ref_table
.
Usually the referenced columns are a primary key in ref_table
.
However, it is also possible to specify other columns via the ref_columns
argument.
If check == TRUE
, then it will first check if the values in columns
are a subset
of the values of the key in table ref_table
.
dm_add_fk(
dm,
table,
columns,
ref_table,
ref_columns = NULL,
...,
check = FALSE,
on_delete = c("no_action", "cascade")
)
dm |
A |
table |
A table in the |
columns |
The columns of |
ref_table |
The table which |
ref_columns |
The column(s) of |
... |
These dots are for future extensions and must be empty. |
check |
Boolean, if |
on_delete |
Defines behavior if a row in the parent table is deleted.
- |
It is possible that a foreign key (FK) is pointing to columns that are neither primary (PK) nor explicit unique keys (UK). This can happen
when a FK is added without a corresponding PK or UK being present in the parent table
when the PK or UK is removed (dm_rm_pk()
/dm_rm_uk()
) without first removing the associated FKs.
These columns are then a so-called "implicit unique key" of the referenced table and can be listed via dm_get_all_uks()
.
An updated dm
with an additional foreign key relation.
Other foreign key functions:
dm_enum_fk_candidates()
,
dm_get_all_fks()
,
dm_rm_fk()
nycflights_dm <- dm(
planes = nycflights13::planes,
flights = nycflights13::flights,
weather = nycflights13::weather
)
nycflights_dm %>%
dm_draw()
# Create foreign keys:
nycflights_dm %>%
dm_add_pk(planes, tailnum) %>%
dm_add_fk(flights, tailnum, planes) %>%
dm_add_pk(weather, c(origin, time_hour)) %>%
dm_add_fk(flights, c(origin, time_hour), weather) %>%
dm_draw()
# Keys can be checked during creation:
try(
nycflights_dm %>%
dm_add_pk(planes, tailnum) %>%
dm_add_fk(flights, tailnum, planes, check = TRUE)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.