如何解决dplyr拉取SQL数据时ON子句表名关联错误问题
问题分析
你遇到的是dplyr在生成数据库查询时的列归属追踪bug:连续执行join+重命名操作后,dplyr错误地将global_curriculum_standard_id关联到了curriculum_standard表(该表实际无此列),而非上一次join引入的curriculum_standard_global_curriculum_standard表。这个问题大概率是dplyr 1.1.0版本的已知bug,和你近期的包版本更新直接相关。
解决方案
方案1:调整重命名时机,避免join后立即重命名
将所有重命名操作放到所有join完成后执行,让dplyr能清晰追踪列的来源:
q_curriculumCoverageTree = tbl(dashCon, "curriculum") %>% select(id, code, grade, deleted) %>% inner_join( tbl(dashCon, "curriculum_strand") %>% select(id, curriculum), by = c("id" = "curriculum"), suffix = c("_curriculum", "_strand") ) %>% inner_join( tbl(dashCon, "curriculum_standard") %>% select(id, strand, code, description), by = c("id_strand" = "strand"), suffix = c("_curriculum", "_standard") ) %>% left_join( tbl(dashCon, "curriculum_standard_global_curriculum_standard") %>% select(curriculum_standard_id, global_curriculum_standard_id), by = c("id_standard" = "curriculum_standard_id") ) %>% left_join( tbl(dashCon, "activity_curriculum_global_standards") %>% select(global_standard_id, activity_id), by = c("global_curriculum_standard_id" = "global_standard_id") ) %>% # 所有join完成后统一重命名 rename( gcs_id = global_curriculum_standard_id )
方案2:拆分链式调用为独立变量
将每个join步骤存为单独变量,强制dplyr明确追踪每个中间结果的列归属:
# 1. 基础curriculum数据 curriculum_base <- tbl(dashCon, "curriculum") %>% select(id, code, grade, deleted) # 2. 连接curriculum_strand strand_join <- curriculum_base %>% inner_join( tbl(dashCon, "curriculum_strand") %>% select(id, curriculum), by = c("id" = "curriculum"), suffix = c("_curriculum", "_strand") ) # 3. 连接curriculum_standard standard_join <- strand_join %>% inner_join( tbl(dashCon, "curriculum_standard") %>% select(id, strand, code, description), by = c("id_strand" = "strand"), suffix = c("_curriculum", "_standard") ) %>% rename(id_standard = id) # 4. 连接全局标准关联表 global_join <- standard_join %>% left_join( tbl(dashCon, "curriculum_standard_global_curriculum_standard") %>% select(curriculum_standard_id, global_curriculum_standard_id), by = c("id_standard" = "curriculum_standard_id") ) %>% rename(gcs_id = global_curriculum_standard_id) # 5. 连接活动表 q_curriculumCoverageTree <- global_join %>% left_join( tbl(dashCon, "activity_curriculum_global_standards") %>% select(global_standard_id, activity_id), by = c("gcs_id" = "global_standard_id") )
方案3:修复dplyr版本
这个列归属bug在dplyr 1.1.0版本中出现,后续版本已修复:
- 升级到最新版:执行
install.packages("dplyr") - 临时降级:如果需要回退到稳定版本,用devtools安装旧版:
devtools::install_version("dplyr", version = "1.0.10")
验证方法
修改后执行show_query(q_curriculumCoverageTree),检查最后一个ON子句是否正确引用curriculum_standard_global_curriculum_standard.global_curriculum_standard_id(或重命名后的gcs_id对应的正确表)。
内容的提问来源于stack exchange,提问作者Ben Parker
相关产品推荐
相关产品推荐

