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 columns = 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( planes = nycflights13::planes, flights = nycflights13::flights ) nycflights_dm %>% dm_draw()
%0 flights flights planes planes
nycflights_dm %>% dm_add_pk(planes, tailnum) %>% dm_add_fk(flights, tailnum, planes) %>% dm_draw()
%0 flights flights tailnum planes planes tailnum flights:tailnum->planes:tailnum
dm_nycflights13(cycle = TRUE) %>% dm_rm_fk(flights, dest, airports) %>% dm_draw()
%0 airlines airlines carrier airports airports faa flights flights carrier tailnum origin flights:carrier->airlines:carrier flights:origin->airports:faa planes planes tailnum flights:tailnum->planes:tailnum weather weather