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

标签 r postgresql postgis r-dbi

当使用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/

相关文章:

正则表达式提取与 R 中某些单词匹配的部分字符串

postgresql - 在事务完成后执行触发器

windows - 在 Windows 7 中使用 Laravel 备份和恢复 PostgreSQL 数据库并设置 localhost 环境

postgresql - 如何在 Postgresql/Postgis 中创建新的 SRID?

python - Django - 以geoJSON格式获取多边形的质心

postgis - 将线串与 PostgreSQL/Postgis 中的 group_by 通用属性合并

r - 将数据帧分成相等的部分

r - 具有可选依赖项的 CRAN 包

r - 停止表格从观星者的页面末尾消失

sql - 如何在 COUNT 聚合中包含 "zero"/"0"结果?