dm_add_pk()
marks the specified columns as the primary key of the specified table.
If check == TRUE
, then it will first check if
the given combination of columns is a unique key of the table.
If force == TRUE
, the function will replace an already
set key, without altering foreign keys previously pointing to that primary key.
Arguments
- dm
A
dm
object.- table
A table in the
dm
.- columns
Table columns, unquoted. To define a compound key, use
c(col1, col2)
.- ...
These dots are for future extensions and must be empty.
- autoincrement
If
TRUE
, the column specified incolumns
will be populated automatically with a sequence of integers.- check
Boolean, if
TRUE
, a check is made if the combination of columns 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. IfTRUE
, a potential oldpk
is deleted before setting a new one.
Details
There can be only one primary key per table in a dm
.
It's possible though to set an unlimited number of unique keys using dm_add_uk()
or adding foreign keys pointing to columns other than the primary key columns with dm_add_fk()
.
See also
Other primary key functions:
dm_add_uk()
,
dm_get_all_pks()
,
dm_get_all_uks()
,
dm_has_pk()
,
dm_rm_pk()
,
dm_rm_uk()
,
enum_pk_candidates()
Examples
nycflights_dm <- dm(
planes = nycflights13::planes,
airports = nycflights13::airports,
weather = nycflights13::weather
)
nycflights_dm %>%
dm_draw()
# Create primary keys:
nycflights_dm %>%
dm_add_pk(planes, tailnum) %>%
dm_add_pk(airports, faa, check = TRUE) %>%
dm_add_pk(weather, c(origin, time_hour)) %>%
dm_draw()
# Keys can be checked during creation:
try(
nycflights_dm %>%
dm_add_pk(planes, manufacturer, check = TRUE)
)
#> Error in abort_not_unique_key(x_label, orig_names) :
#> (`manufacturer`) not a unique key of `planes`.