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

dbWriteTable(temporary = TRUE, overwrite = TRUE) deletes non-temporary table! #402

Closed
mgirlich opened this issue Sep 8, 2022 · 1 comment · Fixed by #431
Closed

dbWriteTable(temporary = TRUE, overwrite = TRUE) deletes non-temporary table! #402

mgirlich opened this issue Sep 8, 2022 · 1 comment · Fixed by #431
Labels

Comments

@mgirlich
Copy link

mgirlich commented Sep 8, 2022

It is possible to create a temporary table with the same name as an existing non-temporary table. Unfortunately, with dbWriteTable() this is not possible and the existing table is simply deleted.

 found <- dbExistsTable(conn, name)
  if (found && !overwrite && !append) {
    stop("Table ", name, " exists in database, and both overwrite and",
      " append are FALSE",
      call. = FALSE
    )
  }
  if (found && overwrite) {
    dbRemoveTable(conn, name)
  }
@krlmlr
Copy link
Member

krlmlr commented Apr 1, 2023

Thanks, confirmed.

library(DBI)
con <- DBI::dbConnect(RPostgres::Postgres())

dbWriteTable(con, "my", data.frame(a = 1), overwrite = TRUE)

dbWriteTable(con, "my", data.frame(b = 2), overwrite = TRUE, temporary = TRUE)
dbReadTable(con, "my")
#>   b
#> 1 2

dbRemoveTable(con, "my", temporary = TRUE)
dbReadTable(con, "my")
#> Error: Failed to prepare query : ERROR:  relation "my" does not exist
#> LINE 1: SELECT * FROM  "my"
#>                        ^

dbRemoveTable(con, "my")
#> Error: Failed to fetch row : ERROR:  table "my" does not exist

dbDisconnect(con)

Created on 2023-04-01 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants