dm_add_fk() marks the specified columns as the foreign key of table table with respect to the primary key of table ref_table. If check == TRUE, then it will first check if the values in columns columns are a subset of the values of the primary key in table ref_table.

dm_rm_fk() can remove either one reference between two tables, or all references at once, if argument column = NULL. All arguments may be provided quoted or unquoted.

dm_add_fk(dm, table, columns, ref_table, check = FALSE)

dm_rm_fk(dm, table, columns, ref_table)

Arguments

dm

A dm object.

table

A table in the dm.

columns

For dm_add_fk(): The columns of table which are to become the foreign key columns that reference the primary key of ref_table.

For dm_rm_fk(): The columns of table that should no longer be referencing the primary key of ref_table. If NULL, all columns will be evaluated.

ref_table

For dm_add_fk(): The table which table will be referencing. This table needs to have a primary key set.

For dm_rm_fk(): The table that table is referencing.

check

Boolean, if TRUE, a check will be performed to determine if the values of column are a subset of the values of the primary key column of ref_table.

Value

For dm_add_fk(): An updated dm with an additional foreign key relation.

For dm_rm_fk(): An updated dm without the given foreign key relation.

Compound keys

Currently, keys consisting of more than one column are not supported. This feature is planned for dm 0.2.0. The syntax of these functions will be extended but will remain compatible with current semantics.

See also

Other foreign key functions: dm_enum_fk_candidates(), dm_get_all_fks(), dm_get_fk(), dm_has_fk()

Other foreign key functions: dm_enum_fk_candidates(), dm_get_all_fks(), dm_get_fk(), dm_has_fk()

Examples

nycflights_dm <- dm_from_src(dplyr::src_df(pkg = "nycflights13")) nycflights_dm %>% dm_draw() nycflights_dm %>% dm_add_pk(planes, tailnum) %>% dm_add_fk(flights, tailnum, planes) %>% dm_draw() dm_rm_fk( dm_nycflights13(cycle = TRUE), flights, dest, airports )
#> ── Table source ──────────────────────────────────────────────────────────────── #> src: <environment: R_GlobalEnv> #> ── Metadata ──────────────────────────────────────────────────────────────────── #> Tables: `airlines`, `airports`, `flights`, `planes`, `weather` #> Columns: 53 #> Primary keys: 3 #> Foreign keys: 3