Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwrite in copy_to with in_schema #489

Closed
gregleleu opened this issue Jul 28, 2020 · 0 comments
Closed

Overwrite in copy_to with in_schema #489

gregleleu opened this issue Jul 28, 2020 · 0 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@gregleleu
Copy link

Overwrite doesn't work in copy_to with a schema because db_has_table which is used to check if the table exists here...

if (overwrite && !is_false(db_has_table(con, table))) {

... wraps dbExistsTable for DBIConnection here...

db_has_table.DBIConnection <- function(con, table) dbExistsTable(con, table)

which uses DBI::Id to handle schemas. But the rest of copy_to needs to be used with dbplyr::in_schema. Should/could some type of conversion happen between the two? Am I missing a simpler version? I have seen the comments saying to just use dbWriteTable instead but I think it would be great if copy_to could handle schemas all the way.

I'm putting a reprex with RSQLite because it's easier but I use RPostgres.

In a sense similar to #239 I believe, but I don't get where that went.

library(tidyverse)
library(magrittr)
library(DBI)

con <- dbConnect(RSQLite::SQLite(), ":memory:")

con %>% dbExecute("ATTACH ':memory:' AS foo")

mtcars_rm <- con %>% copy_to(df = mtcars, name = dbplyr::in_schema("foo", "mtcars"), temporary = FALSE)
mtcars_rm <- con %>% copy_to(df = mtcars, name = dbplyr::in_schema("foo", "mtcars"), temporary = FALSE, overwrite = TRUE) #fails

con %>% db_drop_table("mtcars")
mtcars_rm <- con %>% copy_to(df = mtcars, name =  Id(schema = "foo", table = "mtcars"), temporary = FALSE) # fails


db_has_table(con, dbplyr::in_schema("foo", "mtcars")) # used in practice to check for existence
db_has_table(con, Id(schema = "foo", table = "mtcars")) # right way to check
@hadley hadley added the bug an unexpected problem or unintended behavior label Sep 16, 2020
@hadley hadley closed this as completed in e00de1c Sep 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants