简体   繁体   中英

Adding local tibble to Postgres database where a column is an interval

I'm attempting to add an entry to a Postgres table where one of the columns is an interval. However, I'm unsure how to cast a local tibble's column type as an interval. As you might imagine, type differences make for unhappy databases. Any way to resolve this?

library(dbplyr)
library(dplyr)

con <- my_db_con

df1 <- tibble(my_interval = "P3D")

df2 <- tbl(con, sql("select 'P3D'::interval as my_interval"))

# Will error:
rows_upsert(df2, df1, copy = TRUE)

The resultant error:

Matching, by = "my_interval"
Error: Failed to prepare query: ERROR:  operator does not exist: text = interval
LINE 9:       WHERE ("LHS"."my_interval" = "RHS"."my_interval")
                                         ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

Thank you for your time.

I (hopefully) fixed this in the dev version of dbplyr. Try it out via devtools::install_github("tidyverse/dbplyr") . If this does not work please let me know. There are two other possibilities for you to avoid this issue:

  1. Explicitly copy the table and pass the column types via the types argument
copy_to(my_db_con, df1, types = c(my_interval = "interval"))
  1. You can explicitly copy and then use mutate()
df2_copied <- copy_to(my_db_con, df1) %>% mutate(my_interval = sql("my_interval::interval"))

rows_upsert(df1, df2_copied)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM