当使用dbReadTable
读取使用UUID作为主键的数据库表时,我收到以下警告消息。
1: In postgresqlExecStatement(conn, statement, ...) : RS-DBI driver warning: (unrecognized PostgreSQL field type uuid (id:2950) in column 0)
当我修改加载的表并尝试使用更新数据库时,收到以下错误消息:
Error in postgresqlExecStatement(conn, statement, ...) : RS-DBI driver: (could not Retrieve the result : ERROR: operator does not exist: uuid = text
我知道 UUID 类型在 R 中不可用,但是有没有办法让数据库相信字符向量“unique_id”是 UUID 而不是文本?
代码:
library(RPostgreSQL)
library(postGIStools)
pgdrv <- dbDriver(drvName = "PostgreSQL")
# === open connection
db <- DBI::dbConnect(pgdrv,
dbname="database",
host="localhost", port=5432,
user = 'postgres')
# === get tables
users <- dbReadTable(db, "app_users")
# === interaction with tables
users$employee_has_quit[1:5] <- TRUE
# === update tables
postgis_update(conn = db,
df = users,
tbl = "app_users",
id_cols = "unique_id",
update_cols = "employee_has_quit")
# === close conncetion
DBI::dbDisconnect(db)
最佳答案
问题是 postGIStools 中的错误。您可以看到他们使用的代码 generate this error here
query_text <- paste(query_text, ") AS", tbl_tmp, "(",
paste(quote_id(colnames(df)), collapse = ", "), ")",
"WHERE", paste(paste0(tbl_q, ".", id_q), "=",
paste0(tbl_tmp, ".", id_q),
collapse = " AND "))
简单地说,这是行不通的。他们应该起诉占位符。它假设 input type can be the result of make_str_quote
(by proxy of df_q
and quote_str
) 。这是一个错误的假设,如下所示,
CREATE TABLE foo ( a uuid );
INSERT INTO foo VALUES ( quote_literal(gen_random_uuid()) ) ;
ERROR: column "a" is of type uuid but expression is of type text
LINE 1: INSERT INTO foo VALUES ( quote_literal(gen_random_uuid()) ) ...
^
HINT: You will need to rewrite or cast the expression.
我的建议是您遵循文档,
Note: This package is deprecated. For new projects, we recommend using the
sf
package to interface with geodatabases.
您也许可以解决 this by doing this 问题
CREATE CAST (varchar AS uuid)
WITH INOUT
AS IMPLICIT;
关于RPostgreSQL 和 DBI : "operator does not exist: uuid = text",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52567320/