% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/db-interface.R
\name{copy_dm_to}
\alias{copy_dm_to}
\title{Copy data model to data source}
\usage{
copy_dm_to(
  dest,
  dm,
  ...,
  set_key_constraints = TRUE,
  table_names = NULL,
  temporary = TRUE,
  schema = NULL,
  progress = NA,
  unique_table_names = NULL,
  copy_to = NULL
)
}
\arguments{
\item{dest}{An object of class \code{"src"} or \code{"DBIConnection"}.}

\item{dm}{A \code{dm} object.}

\item{...}{These dots are for future extensions and must be empty.}

\item{set_key_constraints}{If \code{TRUE} will mirror
the primary, foreign, and unique key constraints
and create indexes for foreign key constraints
for the primary and foreign keys in the \code{dm} object.
Set to \code{FALSE} if your data model currently does not satisfy
primary or foreign key constraints.}

\item{table_names}{Desired names for the tables on \code{dest}; the names within the \code{dm} remain unchanged.
Can be \code{NULL}, a named character vector, or a vector of \code{\link[DBI:Id]{DBI::Id}} objects.

If left \code{NULL} (default), the names will be determined automatically depending on the \code{temporary} argument:
\enumerate{
\item \code{temporary = TRUE} (default): unique table names based on the names of the tables in the \code{dm} are created.
\item \code{temporary = FALSE}: the table names in the \code{dm} are used as names for the tables on \code{dest}.
}

If a function or one-sided formula, \code{table_names} is converted to a function
using \code{\link[rlang:as_function]{rlang::as_function()}}.
This function is called with the unquoted table names of the \code{dm} object
as the only argument.
The output of this function is processed by \code{\link[DBI:dbQuoteIdentifier]{DBI::dbQuoteIdentifier()}},
that result should be a vector of identifiers of the same length
as the original table names.

Use a variant of
\code{table_names = ~ DBI::SQL(paste0("schema_name", ".", .x))}
to specify the same schema for all tables.
Use \code{table_names = identity} with \code{temporary = TRUE}
to avoid giving temporary tables unique names.

If a named character vector,
the names of this vector need to correspond to the table names in the \code{dm},
and its values are the desired names on \code{dest}.
The value is processed by \code{\link[DBI:dbQuoteIdentifier]{DBI::dbQuoteIdentifier()}},
that result should be a vector of identifiers of the same length
as the original table names.

Use qualified names corresponding to your database's syntax
to specify e.g. database and schema for your tables.}

\item{temporary}{If \code{TRUE}, only temporary tables will be created.
These tables will vanish when disconnecting from the database.}

\item{schema}{Name of schema to copy the \code{dm} to.
If \code{schema} is provided, an error will be thrown if \code{temporary = FALSE} or
\code{table_names} is not \code{NULL}.

Not all DBMS are supported.}

\item{progress}{Whether to display a progress bar, if \code{NA} (the default)
hide in non-interactive mode, show in interactive mode. Requires the
'progress' package.}

\item{unique_table_names, copy_to}{Must be \code{NULL}.}
}
\value{
A \code{dm} object on the given \code{src} with the same table names
as the input \code{dm}.
}
\description{
\code{copy_dm_to()} takes a \code{\link[dbplyr:src_dbi]{dbplyr::src_dbi}} object or a \code{\link[DBI:DBIConnection-class]{DBI::DBIConnection}} object as its first argument
and a \code{\link{dm}} object as its second argument.
The latter is copied to the former.
The default is to create temporary tables, set \code{temporary = FALSE} to create permanent tables.
Unless \code{set_key_constraints} is \code{FALSE}, primary key, foreign key, and unique constraints
are set, and indexes for foreign keys are created, on all databases.
}
\examples{
\dontshow{if (rlang::is_installed(c("RSQLite", "nycflights13", "dbplyr"))) withAutoprint(\{ # examplesIf}
con <- DBI::dbConnect(RSQLite::SQLite())

# Copy to temporary tables, unique table names by default:
temp_dm <- copy_dm_to(
  con,
  dm_nycflights13(),
  set_key_constraints = FALSE
)

# Persist, explicitly specify table names:
persistent_dm <- copy_dm_to(
  con,
  dm_nycflights13(),
  temporary = FALSE,
  table_names = ~ paste0("flights_", .x)
)
dbplyr::remote_name(persistent_dm$planes)

DBI::dbDisconnect(con)
\dontshow{\}) # examplesIf}
}
\concept{DB interaction functions}
