Skip to contents

Checks that all values in a foreign key column exist in the referenced table's primary key column. Returns a tibble of orphan rows that fail the check.

Usage

validate_fk_references(con, data_tbl, fk_col, ref_tbl, ref_col, label = NULL)

Arguments

con

DuckDB connection

data_tbl

Table name or lazy tbl containing foreign key column

fk_col

Name of the foreign key column to validate

ref_tbl

Name of the referenced table containing the primary key

ref_col

Name of the primary key column in the referenced table

label

Optional label for error messages (default: "data_tbl.fk_col")

Value

Tibble with orphan rows (rows where fk_col value not in ref_tbl.ref_col). Contains all columns from data_tbl where the reference fails.

Examples

if (FALSE) { # \dontrun{
con <- get_duckdb_con()
orphans <- validate_fk_references(
  con     = con,
  data_tbl = "ichthyo",
  fk_col   = "species_id",
  ref_tbl  = "species",
  ref_col  = "species_id")

if (nrow(orphans) > 0) {
  warning(glue("Found {nrow(orphans)} orphan species_id values"))
}
} # }