dm (version 0.0.3.9003)

cdm_add_pk: Mark a column of a table in a dm object as its primary key

Description

cdm_add_pk() marks the given column as the given table's primary key in the data_model-part of the dm object. If check == TRUE, it also first checks if the given column is a unique key of the table. If force == TRUE, it replaces an already set key.

Usage

cdm_add_pk(dm, table, column, check = FALSE, force = FALSE)

Arguments

dm

A dm object.

table

A table in the dm

column

A column of that table

check

Boolean, if TRUE (default), a check is made if the column is a unique key of the table.

force

Boolean, if FALSE (default), an error will be thrown, if there is already a primary key set for this table. If TRUE a potential old pk is deleted before setting the new one.

See Also

Other primary key functions: cdm_get_all_pks, cdm_get_pk, cdm_has_pk, cdm_rm_pk, enum_pk_candidates

Examples

Run this code
# NOT RUN {
library(dplyr)


nycflights_dm <- dm(src_df(pkg = "nycflights13"))

# the following works
cdm_add_pk(nycflights_dm, planes, tailnum)
cdm_add_pk(nycflights_dm, airports, faa)
cdm_add_pk(nycflights_dm, planes, manufacturer, check = FALSE)

# the following does not work
try(cdm_add_pk(nycflights_dm, planes, manufacturer))
# }

Run the code above in your browser using DataLab