RPostgreSQL 和 DBI:"operator does not exist: uuid = text"

RPostgreSQL and DBI: "operator does not exist: uuid = text"

当使用 dbReadTable 读取使用 UUID 作为主键的数据库 tables 时,我收到以下警告消息。

1: In postgresqlExecStatement(conn, statement, ...) : RS-DBI driver warning: (unrecognized PostgreSQL field type uuid (id:2950) in column 0)

当我修改 table 我加载并尝试使用更新数据库时,我收到以下错误消息:

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. You can see the code they're using to 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;