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
.
Usage
dm_add_fk(
dm,
table,
columns,
ref_table,
ref_columns = NULL,
...,
check = FALSE,
on_delete = c("no_action", "cascade")
)
Arguments
- dm
A
dm
object.- table
A table in the
dm
.- columns
The columns of
table
which are to become the foreign key columns that referenceref_table
. To define a compound key, usec(col1, col2)
.- ref_table
The table which
table
will be referencing.- ref_columns
The column(s) of
table
which are to become the referenced column(s) inref_table
. By default, the primary key is used. To define a compound key, usec(col1, col2)
.- ...
These dots are for future extensions and must be empty.
- check
Boolean, if
TRUE
, a check will be performed to determine if the values ofcolumns
are a subset of the values of the key column(s) ofref_table
.- on_delete
-
Defines behavior if a row in the parent table is deleted. -
"no_action"
, the default, means that no action is taken and the operation is aborted if child rows exist -"cascade"
means that the child row is also deleted This setting is picked up bycopy_dm_to()
withset_key_constraints = TRUE
, and bydm_sql()
, and might be considered bydm_rows_delete()
in a future version.
Details
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()
.
See also
Other foreign key functions:
dm_enum_fk_candidates()
,
dm_get_all_fks()
,
dm_rm_fk()
Examples
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)
)
#> Error in abort_not_subset_of(table_name, col_name, ref_table_name, ref_col_name) :
#> Column (`tailnum`) of table `flights` contains values (see examples above) that are not present in column (`tailnum`) of table `planes`.