R - RPostgreSQL 包 - dbWriteTable 到非默认模式,其中目标 table 包含比数据帧更多的字段
R - RPostgreSQL Package - dbWriteTable to non-default schema where target table contains more fields than dataframe
问题
我正在尝试将 R 数据帧 df
的内容复制到位于架构 schema_name
中的 PostgreSQL table table_name
。默认情况下,PostgreSQL 会将 table 写入 public
模式,我 不想 更改此设置。此转移的两个独特方面是:
- 在非默认架构下写入 table;和
- 数据帧
df
包含的字段数量少于 table_name
。 所有 df
中包含的字段,但是 table_name
中确实存在。
我试过的
我首先尝试使用 RPostgreSQL
包中的 dbWriteTable
,方法是:
dbWriteTable(con, c("schema_name","table_name"), df, append = T)
导致以下异常:
Error in postgresqlgetResult(new.con) :
RS-DBI driver: (could not Retrieve the result : ERROR: missing data for column "extra_col"
CONTEXT: COPY df, line 1: " [removed contents] "
然后我尝试从 caroline
包(上述 dbWriteTable
函数的包装器)向我们 dbWriteTable2
,但上面使用的非默认模式 hack 似乎没有工作:
dbWriteTable2(con, c("schema_name","table_name"), df, append = T, add.id = FALSE)
创建以下异常:
creating NAs/NULLs for for fields of table that are missing in your df
Error in postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not Retrieve the result : ERROR: relation "schema_name" does not exist
LINE 1: SELECT * FROM schema_name ORDER BY id DESC LIMIT 1
在查询之前添加缺少的空字段:
df$extr_col1 <- NA
df$extr_col2 <- NA
...
然后 运行 你原来的 dbWriteTable()
...
问题
我正在尝试将 R 数据帧 df
的内容复制到位于架构 schema_name
中的 PostgreSQL table table_name
。默认情况下,PostgreSQL 会将 table 写入 public
模式,我 不想 更改此设置。此转移的两个独特方面是:
- 在非默认架构下写入 table;和
- 数据帧
df
包含的字段数量少于table_name
。 所有df
中包含的字段,但是table_name
中确实存在。
我试过的
我首先尝试使用 RPostgreSQL
包中的 dbWriteTable
,方法是:
dbWriteTable(con, c("schema_name","table_name"), df, append = T)
导致以下异常:
Error in postgresqlgetResult(new.con) :
RS-DBI driver: (could not Retrieve the result : ERROR: missing data for column "extra_col"
CONTEXT: COPY df, line 1: " [removed contents] "
然后我尝试从 caroline
包(上述 dbWriteTable
函数的包装器)向我们 dbWriteTable2
,但上面使用的非默认模式 hack 似乎没有工作:
dbWriteTable2(con, c("schema_name","table_name"), df, append = T, add.id = FALSE)
创建以下异常:
creating NAs/NULLs for for fields of table that are missing in your df
Error in postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not Retrieve the result : ERROR: relation "schema_name" does not exist
LINE 1: SELECT * FROM schema_name ORDER BY id DESC LIMIT 1
在查询之前添加缺少的空字段:
df$extr_col1 <- NA
df$extr_col2 <- NA
...
然后 运行 你原来的 dbWriteTable()
...